简体   繁体   中英

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. 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. observed appid is missing in your code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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