JSON REST Service: Content-Encoding: gzip

2019-01-28 13:03发布

问题:

I am writing some code to interface with a service that I do not have yet, so I am writing a simulator to attempt to de-risk some of the problems I might run into when I get a chance to integrate against the real system. The interface is basically a REST style interface that returns JSON formatted strings.
The interface specification says the JSON formatted response is returned in lieu of the standard HTTP body. It also says that responses from the server will be zlib compressed and have the "Content-Encoding: gzip" set in the header. So I created a WCF service that provides a REST interface that returns a JSON formatted string. I now need to deal with the compression portion of the equation. To satisfy the Content-Encoding: gzip requirements, do I simply gzip the JSON string I created and return that rather than the string? Or is it more involved than that? Let me know if there is any other information that is needed here, as I am still a newbie when dealing with REST/HTTP.

Thanks so Much for your time.

回答1:

You're correct. Just Gzip the JSON string and return it.

Best reference for any REST implementation is the HTTP/1.1 RFC: http://tools.ietf.org/html/rfc2616



回答2:

In short: yes, it's as simple as that. The response body just needs to be the gzip-compressed version of the normal response body.

This question may have some useful information for setting up your service.