Spray Client Http Header content type

658 views
Skip to first unread message

Jonathan Knight

unread,
May 21, 2014, 11:10:06 AM5/21/14
to spray...@googlegroups.com
I am calling an API using the client pipeline. The API returns a content type of "xml; charset=utf-8". The unmarshaller is failing because this isn't a defined content type. When I tried to set this up as a custom content type it also failed becuase it has only one part. Any way I can get the API call to work ?

 

Johannes Rudolph

unread,
May 21, 2014, 11:19:56 AM5/21/14
to spray...@googlegroups.com
Hi Jonathan,
You can write a function

def fixResponse(response: HttpResponse): HttpResponse = // ...

which rewrites responses with the broken content-type header to use a
correct one i.e. it sets the content-type of the HttpEntity correctly.
You can then add this method into the pipeline before the
unmarshaller.

... ~> fixResponse ~> unmarshal[...]

HTH


--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Jonathan Knight

unread,
May 21, 2014, 12:55:23 PM5/21/14
to spray...@googlegroups.com, johannes...@googlemail.com
Hi Johannes,

Yes I am trying that - but can't seem to get a writable header

Jonathan Knight

unread,
May 21, 2014, 3:08:16 PM5/21/14
to spray...@googlegroups.com, johannes...@googlemail.com
OK Here is my function - but it is not working because I get an error before it is called

    val alterContentType: HttpResponse => HttpResponse = {r =>
      val c = HttpHeaders.`Content-Type`(ContentType(`text/xml`))
      r.withHeaders(r.headers.updated(r.headers.indexWhere(p => p.is("content-type")), c))
    }

and my pipeline is

sendReceive ~> alterContentType  ~> unmarshal

But I still get the following:

 Illegal response header: Illegal 'Content-Type' header: Invalid input ';', expected TokenChar or '/' (line 1, pos 4):
19:58:13 local.1 | xml; charset=utf-8

and then

spray.httpx.PipelineException: UnsupportedContentType(Expected 'text/xml' or 'application/xml' or 'text/html' or 'application/xhtml+xml')



Amir Gur

unread,
Jul 1, 2014, 3:37:53 PM7/1/14
to spray...@googlegroups.com
Any resolution here?

Getting the same issue when the web-app application attempts to Set-Cookie to one with a colon ':'.  If it's officially illegal by RFC or not should not matter, since sites have always been not careful with following the rules, and browsers have always been forgiving and have been accepting variations from RFCs for years.  Now surprise surprise, same goes here too, they (checked chrome) accept the the colon ':' in the Set-Cookie of the response.
A library being strict on RFC or not should be a flag controlling two modes which should be easy to switch between.  One mode would be strict, one not.  The not strict mode should follow whatever the browsers do.  Then such an OSS lib also got a chance to be adopted by the browser's future versions.

My output is: 

[WARN] [07/01/2014 12:13:24.799] [spray-http-client-akka.actor.default-dispatcher-9] [akka://spray-http-client/user/IO-HTTP/group-0/2] Illegal response header: Illegal 'Set-Cookie' header: Invalid input ':', expected TokenChar or '=' (line 1, pos 10):
<alpha-only>:<numbers-only>=<alpha-only>:<numbers-only>; domain=<some-ip>; expires=Wed, 01-Jul-2015 19:13:28 GMT; path=/
            ^

Mathias Doenitz

unread,
Jul 2, 2014, 7:26:52 AM7/2/14
to spray...@googlegroups.com
Amir,

spray does accept the message but leaves the `Set-Cookie` header unparsed in such cases.
I.e. you can still access it as a `RawHeader`.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/718e9f34-912f-4b72-967f-4c7fc743c09e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages