How can ignore a BOM on a XML response I am trying to consume?

523 views
Skip to first unread message

djangofan

unread,
May 31, 2012, 2:11:11 PM5/31/12
to rest-a...@googlegroups.com
I am using RestAssured to consume a web service running on Websphere.   Most of the time the response is clean, UTF-8, without a BOM header but sometimes the response contains a header and footer like what you see below.  Is there a way to override the "post" method so that I can strip the BOM before consuming it?    Is there already a feature in RestAssured that gives me this ability?  

HTTP/1.1 200 OK
Date: Thu, 31 May 2012 17:41:20 GMT
Server: WebSphere Application Server/6.1
Content-Type: text/xml; charset=utf-8
Content-Language: en-US

733
<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  ...
  ...
</soapenv:Envelope>
0

Johan Haleby

unread,
May 31, 2012, 3:07:26 PM5/31/12
to rest-a...@googlegroups.com
Hi, 

Hmm interesting question. I've also had problems with BOM's in past (not with Rest Assured though) so I know how annoying it can be..  I don't really know how you can solve this. One approach you could try (but I'm not sure if it'll work) is to create a filter in Rest Assured that gets the response body as an inputstream and then wrap it in a BOMInputStream (and if wrapping doesn't work you could try to copy the content of the first inputstream to the BOMInputStream as well) from Apache Commons IO and return a new response with this inputstream.

Regards,
/Johan

Jon Thor Austen

unread,
May 31, 2012, 5:57:57 PM5/31/12
to rest-a...@googlegroups.com
Johan,

The problem is that I am using RestAssured like this:

String xml = given().request().headers( authhdrs
).contentType("application/soap+xml; charset=UTF-8;")
.body(myEnvelope).when().post( "/ws/services/Service" )
.andReturn().asString();

Now, what I really need is a andReturn() method that is able to strip
the BOM. Is it possible to extend a class and override the
andReturn() method so that I can strip the BOM? Perhaps the
resulting code would enable this:

String xml = given().request().headers( authhdrs
).contentType("application/soap+xml; charset=UTF-8;")
.body(myEnvelope).when().post( "/ws/services/Service" )
.andReturnWithoutBOM().asString();

If I can't solve, I will have to abandon RestAssured and code AXIS
manually to handle the response.

-Jon

Johan Haleby

unread,
Jun 1, 2012, 1:56:29 AM6/1/12
to rest-a...@googlegroups.com
Hi,

I've looked at this a bit more closely and Rest Assured actually works with UTF-8 BOMs, at least in my test case (look at the BomITest in the source code and the "xmlWithBom" resource in the ScalatraRestExample class and you can see an example of this). What you mean is perhaps not a BOM? What error message do you get in Rest Assured? What version are you using? (if you're using 1.5 or below you really ought to upgrade)

With my suggestion using filters you could be able to "override" andReturn and strip out the "BOM" (or header and footer) before it's returned to your code. You could apply this filter once using: 

RestAssured.filters(myHeaderAndFooterRemovingFilter);

The filter would then return a well-formatted XML that you'll receive when you do ".andReturn().asString()"

Regards,
/Johan

djangofan

unread,
Jun 1, 2012, 12:30:17 PM6/1/12
to rest-a...@googlegroups.com
Johan,

Your reply was very helpful but it turns out that what I am dealing with here is not a BOM, but rather it is a HTTP1.1 "chunked encoding" with a "last-chunk=0" being included.  (  http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html )  .   So, the question now is what am I doing wrong that ArrestAssured is not handling the chunked decoding?

"When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header."



-Jon

djangofan

unread,
Jun 2, 2012, 2:53:25 PM6/2/12
to rest-a...@googlegroups.com
Ok, I found a small bug in my RestAssured script that was causing my SOAP request payload to be corrupted.  So, it turns out that RestAssured handles the chunking invisibly/seamlessly .  Thanks, I am all working now and I learned a lot from this thread.
 
-Jon

Johan Haleby

unread,
Jun 4, 2012, 2:08:41 AM6/4/12
to rest-a...@googlegroups.com
Hi, 

I'm glad that you got it working. Now I know that REST Assured (thanks to HTTP Client) supports http chunking out of the box.

Regards,
/Johan
Reply all
Reply to author
Forward
0 new messages