简体   繁体   English

Rest 保证总是返回 400 Bad Request for POST

[英]Rest Assured always returning 400 Bad Request for POST

I'm writing a simple Rest Assured test but it always returning 400 Bad Request for POST.我正在编写一个简单的 Rest 保证测试,但它总是返回 400 Bad Request for POST。 I've tried many approaches but no success, can anyone please advise?我尝试了很多方法,但都没有成功,有人可以建议吗?

Approach 1:方法一:

@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);
}

Approach 2:方法二:

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 );

The above is TestNG framework works for me.以上是适用于我的TestNG 框架 observed appid is missing in your code.您的代码中缺少观察到的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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM