簡體   English   中英

請放心:發布請求返回錯誤“JSON 輸入文本既不應該為 null 也不應該為空。”

[英]Rest Assured:Post request returning an error "The JSON input text should neither be null nor empty."

我是 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\"" +
            "}";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM