groovy-wslite
groovy-wslite copied to clipboard
Send JSON, receive stream?
Hi, like the client. Wondering if there is any way to send a JSON payload, and get back a application/octet-stream
?
I'm getting a response parse error:
wslite.rest.RESTContentParseException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
Or alternatively, is there a way to just disable auto JSON parsing? (Problem is, I must send a request that has content-type=application/json
, but wslite seems to assume that that means JSON is always returned?)
This "best-effort" hack seems to help patch it up a little bit:
ResponseBuilder.metaClass.parseJsonContent = { String content ->
return content.trim().startsWith('[') ? new JSONArray(content) :
content.trim().startsWith('{') ? new JSONObject(content) :
null
}
(Not saying this is the optimal solution.....)
Looking at the code, it doesn't look like wslite assumes the return contentType is JSON. It takes the return contentType from HttpUrlConnection.getContentType()
, which should be your application/octet-stream
. And wslite doesn't try to parse the response as JSON unless it really recognizes it as something JSON encoded, eg contentType is one of the following: JSON(['application/json', 'application/hal+json', 'application/javascript', 'text/javascript', 'text/json'])
.
Are you sure your server responds with a Content-Type: application/octet-stream
header?