【发布时间】:2021-02-09 07:43:16
【问题描述】:
我正在编写一个简单的 Rest Assured 测试,但它总是返回 400 Bad Request for POST。我尝试了很多方法,但都没有成功,有人可以建议吗?
方法一:
@When("^User register a station successfully response code is 201$")
public void User_register_a_station_successfully_response_code_is_201() throws Throwable {
given()
.header("Key","REDACTED")
.header("Content-Type","application/json")
.param("external_id", "DEMO_TEST001")
.param("name", "Team Demo Test Station 001")
.param("latitude", "33.33")
.param("longitude", "-122.43")
.param("altitude", "222")
.post("/http://api.openweathermap.org/data/3.0/stations?appid=c418f61b4fb72d7e608bac74d6660b")
.then().statusCode(201);
given()
.header("key","REDACTED")
.header("Content-Type","application/json")
.param("external_id", "DEMO_TEST002")
.param("name", "Team Demo Test Station 002")
.param("latitude", "44.44")
.param("longitude", "-122.44")
.param("altitude", "111")
.post("http://api.openweathermap.org/data/3.0/stations?appid=c418f61b4fb72d7e608bac74d6660b")
.then().statusCode(201);
}
方法二:
RestAssured.baseURI ="http://api.openweathermap.org/data/3.0/stations?appid=c418f61b4fb72d7e608bac74d6660b";
JSONObject request = new JSONObject();
request.put("external_id", "DEMO_TEST001");
request.put("name", "Team Demo Test Station 001");
request.put("latitude", "33.33");
request.put("longitude", "-122.43");
request.put("altitude", "222");
given().header("Content-Type","application/json").
body(request.toJSONString()).
when().
post(RestAssured.baseURI).
then().statusCode(201 );
【问题讨论】:
-
显示您的回复,问题肯定出在请求中。您确定在 POST 期间不需要发送正文吗?
标签: java cucumber rest-assured