groovy-wslite icon indicating copy to clipboard operation
groovy-wslite copied to clipboard

Send JSON, receive stream?

Open crazy4groovy opened this issue 8 years ago • 2 comments

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?)

crazy4groovy avatar Aug 23 '16 18:08 crazy4groovy

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.....)

crazy4groovy avatar Aug 23 '16 19:08 crazy4groovy

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?

boekhold avatar Oct 19 '16 12:10 boekhold