REST-assured always POST application/x-www-form-urlencoded instead of application/json

3,804 views
Skip to first unread message

cl...@ninjavan.sg

unread,
Jan 17, 2016, 12:13:13 PM1/17/16
to REST assured
Hi everyone,

We like what Jayway has done with REST-assured and going to use it but we have tried many times yet REST-assured always POST application/x-www-form-urlencoded content type instead of the required application/json

Following are what we have tried based on what we managed to find out:
RestAssured.config = new RestAssuredConfig().encoderConfig(EncoderConfig.encoderConfig()
        .encodeContentTypeAs("application/json", ContentType.JSON));



given()

    .header("Authorization", "Some token")
    .contentType(ContentType.JSON)
    //.header("Content-Type", "application/json;charset=utf-8")

    //.contentType("application/json")
        
    .body("[{\"scan\":\"Something\", \"type\":\"type1\", \"hubId\":1}]")

Response response = post("http://someServer/2.0/operator/inbounding");

Here are the debug logs snippet:
[debug] - org.apache.http.impl.conn.DefaultClientConnection - Sending request: POST /2.0/operator/inbounding HTTP/1.1
[debug] - org.apache.http.wire -  >> "POST /2.0/operator/inbounding HTTP/1.1[\r][\n]"
[debug] - org.apache.http.wire -  >> "Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1[\r][\n]"
[debug] - org.apache.http.wire -  >> "Accept: */*[\r][\n]"
[debug] - org.apache.http.wire -  >> "Content-Length: 0[\r][\n]"
[debug] - org.apache.http.wire -  >> "Host: api-staging.ninjavan.sg[\r][\n]"
[debug] - org.apache.http.wire -  >> "Connection: Keep-Alive[\r][\n]"
[debug] - org.apache.http.wire -  >> "User-Agent: Apache-HttpClient/4.3.5 (java 1.5)[\r][\n]"
[debug] - org.apache.http.wire -  >> "Accept-Encoding: gzip,deflate[\r][\n]"

Please help. Thank you very much

Regards,
Clyde

Johan Haleby

unread,
Jan 17, 2016, 12:25:22 PM1/17/16
to rest-a...@googlegroups.com
Hmm I'm pretty sure this works (there are several tests in the code based that verifies this). I just tried it as well and looked what's actually sent on the wire using Wireshark. 

given().
        contentType("application/json").
        body("{ \"message\" : \"hello world\"}").
when().
        post("/jsonBody").
then().
        body(equalTo("hello world"));

Gives me the following in Wireshark:

Inline image 1

Using given().header("content-type", "application/json") also works.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cl...@ninjavan.sg

unread,
Jan 18, 2016, 12:15:41 AM1/18/16
to REST assured
Hi Johan,

Thank you very much for the prompt reply

We have managed to figure what exactly is wrong

It works if we do the below:
given()
        
    .header("Authorization", “some token“)

    .contentType(ContentType.JSON)

    .body(“[{something}]”)


.when()
    .post("http://someURL”)

.then()

    .contentType(ContentType.JSON)

    .statusCode(200)

If we do the below, the problem mentioned earlier will occur:
given()

    .header("Authorization", "Some token")
    .contentType(ContentType.JSON)
    .body("[{\"scan\":\"Something\", \"type\":\"type1\", \"hubId\":1}]");

Response response = post("someURL");

By the way, how do we capture the JSON response with the former .given.when.then syntax? We want to separate this to integrate with Cucumber steps

Please feel free to recommend any alternative if you know of better way to do this. Thank you

Regards,
Clyde

Johan Haleby

unread,
Jan 18, 2016, 4:10:43 AM1/18/16
to rest-a...@googlegroups.com
On Mon, Jan 18, 2016 at 6:15 AM, <cl...@ninjavan.sg> wrote:
Hi Johan,

Thank you very much for the prompt reply

We have managed to figure what exactly is wrong

It works if we do the below:
given()
        
    .header("Authorization", “some token“)

    .contentType(ContentType.JSON)

    .body(“[{something}]”)


.when()
    .post("http://someURL”)

.then()

    .contentType(ContentType.JSON)

    .statusCode(200)

If we do the below, the problem mentioned earlier will occur:
given()

    .header("Authorization", "Some token")
    .contentType(ContentType.JSON)
    .body("[{\"scan\":\"Something\", \"type\":\"type1\", \"hubId\":1}]");

Response response = post("someURL");


Well that's definitely expected behavior, you start a new statement when doing post and you do nothing on the first statement. What you want to do is this:

Response response = 
given()

    .header("Authorization", "Some token")
    .contentType(ContentType.JSON)
    .body("[{\"scan\":\"Something\", \"type\":\"type1\", \"hubId\":1}]")
    .post("someURL");

Clyde Tan

unread,
Jan 18, 2016, 4:15:57 AM1/18/16
to REST assured
Thanks a lot Johan for sharing the proper syntax. Will correct it
Reply all
Reply to author
Forward
0 new messages