setting content type in rest assured

后端 未结 8 1199
广开言路
广开言路 2021-01-07 21:53

I\'m trying to invoke a rest call using rest assured. My API accepts, \"application/json\" as content type and I need to set in the call. I set the content type

相关标签:
8条回答
  • 2021-01-07 22:27

    This might possibly be the case with your test. Try this.

    https://github.com/rest-assured/rest-assured/wiki/Usage#avoid-adding-the-charset-to-content-type-header-automatically

    0 讨论(0)
  • 2021-01-07 22:30

    As mentioned in previous posts there is a method:

    RequestSpecification.contentType(String value)

    I did not work for me too. But after upgrade to the newest version (in this moment 2.9.0) it works. So please upgrade :)

    0 讨论(0)
  • 2021-01-07 22:34
    import io.restassured.RestAssured;
    import io.restassured.http.ContentType;
    
    import static org.hamcrest.Matchers.is;
    import org.testng.annotations.Test;
    import static io.restassured.RestAssured.given;
    
    public class googleMapsGetLocation {
        @Test
        public void getLocation() {
            RestAssured.baseURI = "https://maps.googleapis.com";
            given().param("location", "-33.8670522,151.1957362")
                .param("radius", "500")
                .param("key", "AIzaSyAONLkrlUKcoW-oYeQjUo44y5rpME9DV0k").when()
                .get("/maps/api/place/nearbysearch/json").then().assertThat()
                .statusCode(200).and().contentType(ContentType.JSON)
                .body("results[0].name", is("Sydney"));
        }
    }
    
    0 讨论(0)
  • 2021-01-07 22:37

    For your First option can you please try adding this header too and sending the request?

    .header("Accept","application/json")

    0 讨论(0)
  • 2021-01-07 22:38

    Give a try given().contentType(ContentType.JSON).body(inputPayLoad.toString)

    0 讨论(0)
  • 2021-01-07 22:44

    Here is the complete POST example using CONTENT_TYPE as JSON.

    import io.restassured.http.ContentType;
    
    RequestSpecification request=new RequestSpecBuilder().build();
    ResponseSpecification response=new ResponseSpecBuilder().build();
    @Test
    public void test(){
       User user=new User();
       given()
        .spec(request)
        .contentType(ContentType.JSON)
        .body(user)
        .post(API_ENDPOINT)
        .then()
        .statusCode(200).log().all();
    }
    
    0 讨论(0)
提交回复
热议问题