【问题标题】:Rest Assured always returning 400 Bad Request for POST放心总是返回 400 Bad Request for POST
【发布时间】: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


【解决方案1】:

以上是 TestNG 框架 适合我。观察到您的代码中缺少appid

public class WeatherGetReqQueryParam {
@Test public void byCity() {
    Response resp = given().
                param("q","London").
                param("appid", "XXXXXXXX").
                when().get("http://api.openweathermap.org/data/2.5/weather");               
        Assert.assertEquals(resp.getStatusCode(), 200);
        System.out.println(resp.asString());

【讨论】:

    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 2017-11-30
    • 2020-07-29
    • 2023-01-12
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多