java - 请放心 :Post request returning an error "The JSON input text should neither be null nor empty."

标签 java json rest-assured web-api-testing

我是 REST-Api 测试的新手。我正在开始使用 Rest-Assured 进行 Rest-Api 测试,但我在这方面遇到了问题。下面是不断返回上述错误的代码

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import files.resources;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

public class Basics3 {

    Properties prop = new Properties();

    @BeforeTest
    public void getData() throws IOException{

        FileInputStream fis = new FileInputStream("C:\\Users\\boma.dagogo\\Desktop\\BOMA DAGOGO\\eclipse-workspace\\DemoProject\\src\\files\\env.properties");
        prop.load(fis);
        //prop.get("HOST");
    }

    @Test
    public void addandDeletePlace() {

        String b = "{\r\n" +
                "    \"location\":{\r\n" +
                "        \"lat\" : -38.383494,\r\n" +
                "        \"lng\" : 33.427362\r\n" +
                "    },\r\n" +
                "    \"accuracy\":50,\r\n" +
                "    \"name\":\"Frontline house\",\r\n" +
                "    \"phone_number\":\"(+91) 983 893 3937\",\r\n" +
                "    \"address\" : \"29, side layout, cohen 09\",\r\n" +
                "    \"types\": [\"shoe park\",\"shop\"],\r\n" +
                "    \"website\" : \"http://google.com\",\r\n" +
                "    \"language\" : \"French-IN\"\r\n" +
                "}";

        RestAssured.baseURI = prop.getProperty("HOST");
        Response res = given().
                queryParam("key", " qaclick123").
                body(b).
                when().
                post(resources.placePostData()).
                then().assertThat().statusCode(200).and().contentType(ContentType.JSON).and().
                body("status", equalTo("OK")).
                extract().response();

        //Task 2: Grab the place_id from the response

        String responseString = res.asString();
        System.out.println(responseString);

        JsonPath js = new JsonPath(responseString);
        js.get("place_id");

        String place_id = js.get("place_id");
        System.out.println(place_id);

        //Task 3: Place this place_id in the Delete request
        given().
                queryParam("key", prop.getProperty("KEY")).
                body("{" +
                        "\"place_id\":\"" + place_id + "\"" +
                        "}").
                when().
                post("/maps/api/place/delete/json").
                then().assertThat().statusCode(200).and().contentType(ContentType.JSON).and().
                body("status", equalTo("OK"));

    }

}

最佳答案

字符串b有错误。

只需替换以下代码,然后重新运行:

    String b = "{" +
            "\"location\":{" +
            "\"lat\" : -38.383494," +
            "\"lng\" : 33.427362" +
            "}," +
            "\"accuracy\":50," +
            "\"name\":\"Frontline house\"," +
            "\"phone_number\":\"(+91) 983 893 3937\"," +
            "\"address\" : \"29, side layout, cohen 09\"," +
            "\"types\": [\"shoe park\",\"shop\"]," +
            "\"website\" : \"http://google.com\"," +
            "\"language\" : \"French-IN\"" +
            "}";

关于java - 请放心 :Post request returning an error "The JSON input text should neither be null nor empty.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53815790/

相关文章:

java流逗号分隔

json - Angular JS 正确的工厂结构

json - ExtJs - 网格分组问题

java - 为单个测试用例生成 JUnit 报告

java - 我在 : Exception in thread "main" org. hibernate.HibernateException : Could not parse configuration: hibernate. cfg.xml 中收到错误

java - Maven 插件如何在执行期间发现自己的版本?

java - RestAssured 中出现超时错误,而服务在 postman/soapUI 中给出响应

java - 如何使用 JSON 响应通过 Rest Assured 基于正文项进行断言?

java - 解析CSV行出错谁有解决办法

java - GWT 应用程序 404 以托管模式连接到 RPC servlet