RestAssured returns HTML response (which is not correct) but PostMan and Apache HTTPClient returns correct XML response - Why?

1,080 views
Skip to first unread message

prvns

unread,
Sep 23, 2016, 9:26:19 AM9/23/16
to REST assured
I have an XML body which needs to be posted to a non-secure url. So I'm doing the following

               RestAssured.baseURI = url;

               Response response = RestAssured.given().contentType(ContentType.XML).body(body).post();


However The response I receive is an HTML page which is not correct.  If I do the same thing as below

HttpClient httpClient = HttpClientBuilder.create().build();
 
HttpPost httpPost = new HttpPost(url);
 org
.apache.http.entity.StringEntity entity = new org.apache.http.entity.StringEntity(body);
 httpPost
.setEntity(entity);
 httpPost
.setHeader("Accept", "text/xml");
 httpPost
.setHeader("Content-type", "text/xml");
 
HttpResponse httpResponse = httpClient.execute(httpPost);
 
HttpEntity responseEntity = httpResponse.getEntity();
 
if (responseEntity != null) {
 
String responseAsString = EntityUtils.toString(responseEntity);
 
System.out.println(responseAsString);

 
}


This returns an XML response which is correct and the expected response. 


What am I doing wrong? Is there some configuration that needs to be made?

Johan Haleby

unread,
Sep 23, 2016, 10:08:17 AM9/23/16
to rest-a...@googlegroups.com
What does it say when you log the response (log().all())?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Todd Bradley

unread,
Sep 23, 2016, 10:14:32 AM9/23/16
to rest-a...@googlegroups.com
Your first statement (the RA way) didn't tell the server what content type you will accept in the response, only what type you are sending in the request. 
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Sent from Gmail Mobile

prvns

unread,
Sep 23, 2016, 10:57:45 AM9/23/16
to REST assured
This is what I see

Request method: POST
Request URI: http://myurl.do/blah
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Multiparts: <none>
Headers: Accept=*/*
 Content-Type=application/xml; charset=ISO-8859-1
Cookies: <none>
Body:The XML body that I
passed
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

prvns

unread,
Sep 23, 2016, 11:00:56 AM9/23/16
to REST assured
Ok. I tried this 

Response response = RestAssured.given()
 
.accept(ContentType.XML)
 
.contentType(ContentType.XML)
 
.log()
 
.all()
 
.body(body)
 
.post();


Even after that I see the same HTML as response. 


On Friday, September 23, 2016 at 10:14:32 AM UTC-4, Todd Bradley wrote:
Your first statement (the RA way) didn't tell the server what content type you will accept in the response, only what type you are sending in the request. 
I have an XML body which needs to be posted to a non-secure url. So I'm doing the following

               RestAssured.baseURI = url;

               Response response = RestAssured.given().contentType(ContentType.XML).body(body).post();


However The response I receive is an HTML page which is not correct.  If I do the same thing as below

HttpClient httpClient = HttpClientBuilder.create().build();
 
HttpPost httpPost = new HttpPost(url);
 org
.apache.http.entity.StringEntity entity = new org.apache.http.entity.StringEntity(body);
 httpPost
.setEntity(entity);
 httpPost
.setHeader("Accept", "text/xml");
 httpPost
.setHeader("Content-type", "text/xml");
 
HttpResponse httpResponse = httpClient.execute(httpPost);
 
HttpEntity responseEntity = httpResponse.getEntity();
 
if (responseEntity != null) {
 
String responseAsString = EntityUtils.toString(responseEntity);
 
System.out.println(responseAsString);

 
}


This returns an XML response which is correct and the expected response. 


What am I doing wrong? Is there some configuration that needs to be made?

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Todd Bradley

unread,
Sep 23, 2016, 11:09:56 AM9/23/16
to rest-a...@googlegroups.com
Hmm, that's weird. Try .accept("text/xml") specifically.

prvns

unread,
Sep 23, 2016, 11:24:00 AM9/23/16
to REST assured
Still the same response which is HTML. I'm surprised that it works fine in HttpClient though. I get proper XML response. 

I'm using RestAssured version 3.0.1 if that matters. 

To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.

Todd Bradley

unread,
Sep 23, 2016, 11:27:49 AM9/23/16
to rest-a...@googlegroups.com
Well, you've got me baffled. When I get to this point, I fire up Wireshark and compare the actual HTTP request packet between method A and B. There must be some subtle difference that you and I just aren't seeing. And yeah, maybe it's a RA bug.

prvns

unread,
Sep 23, 2016, 11:33:17 AM9/23/16
to REST assured
I agree with you. I'm gonna try wireshark and find out any differences and post here. 

Do you know if there is a way to convert httpclient response to RA response?

Thanks for help me though

Johan Haleby

unread,
Sep 23, 2016, 1:08:36 PM9/23/16
to rest-a...@googlegroups.com
I would also recommend wireshark for these occasions. If you can show us the request and response that you see in wireshark for both HttpClient and REST Assured then we ought to be able to figure out what's wrong.

Todd Bradley

unread,
Sep 23, 2016, 1:17:46 PM9/23/16
to rest-a...@googlegroups.com
And no, there is no way to convert from an HttpClient response to a RA response, or vice versa.

Zach David

unread,
Sep 23, 2016, 1:18:21 PM9/23/16
to REST assured
Hello - I have seen this issue with several apps.  Rest Assured appends "charset=ISO-8859-1" to the Content-Type, which can alter response results.

Try adding:
private static EncoderConfig enConfig = new EncoderConfig();
private static RestAssuredConfig rConfig = RestAssuredConfig.config()
.encoderConfig(enConfig
.appendDefaultContentCharsetToContentTypeIfUndefined(
false));
given()
.header("content-type", "application/xml")
.header(auth)
.config(rConfig)

prvns

unread,
Sep 23, 2016, 1:31:17 PM9/23/16
to REST assured
Found the issue. Thanks for pointing me towards Wireshark. 

I compared the request that was sent from RA and request that was sent from HttpClient.

So this is what is happening. RA is dropping the query string for some reasons.


HttpClient is sending as is. Which is good. But RA is doing a POST with the following URL


Notice the difference? It's dropping the query string from the URL. 

Now what I did is I posted the URL using the HttpClient without the query String (without ?site=2)... BOOM!!! I got the bad HTML response that I was getting in RestAssured.

So to conclude RA is dropping the query string for some reasons.

Todd Bradley

unread,
Sep 23, 2016, 1:45:06 PM9/23/16
to rest-a...@googlegroups.com
On Fri, Sep 23, 2016 at 11:31 AM, prvns <neevar...@gmail.com> wrote:
Found the issue. Thanks for pointing me towards Wireshark. 

You're welcome! Always keep it at the top of your bag of debugging tricks.
 

I compared the request that was sent from RA and request that was sent from HttpClient.

So this is what is happening. RA is dropping the query string for some reasons.


HttpClient is sending as is. Which is good. But RA is doing a POST with the following URL


Does your actual code do this?

.param("site", "2");


prvns

unread,
Sep 23, 2016, 1:53:24 PM9/23/16
to REST assured
No it doesn't. It's just plain URL I'm parsing directly.  

Johan Haleby

unread,
Sep 23, 2016, 2:25:08 PM9/23/16
to rest-a...@googlegroups.com
RA might exclude it from the baseUri (I don't remember to be honest). You should add queryParam("site", "2") since  when you do a post in RA it'll default to form parameters (which are sent in the body).

--
Reply all
Reply to author
Forward
0 new messages