0

I'm using node.js with express. I have my home page which after loading will hit my REST endpoint (PUT) sending some json data.I'm not gziping the data while sending to REST end point.But at my endpoint I want it in gzip form is it possible ? If so how ?

Note: I want to use content-encoding from client. I neither want to gzip it on client nor on server. (Not even as a middleware as that is also on server)

7
  • 1
    You want to use some content encoding from the client? Or, you simply want to store it gzipped on the server?
    – Brad
    Oct 30, 2019 at 6:12
  • I want to use content encoding from client. I don't want to gzip it on server.
    – Geek
    Oct 30, 2019 at 7:02
  • Show what have you done so far?
    – Sohan
    Oct 30, 2019 at 7:21
  • One way is to do using requestPromise github.com/request/request#requestoptions-callback. See gzip option. If you think this can be helpful I can provide more detail
    – Sohan
    Oct 30, 2019 at 8:08
  • @Sohan that can be helpful if my request data comes as gzip to my endpoint.I'm finding many approaches for response data but here I want it on request data which goes from client(browser) to REST endpoint as a PUT request.
    – Geek
    Oct 30, 2019 at 8:21

2 Answers 2

0

You can add a middleware in your express server. Any requests hitting your endpoint will go through this middleware, and within your middleware, you can gzipping your data, this zipped data get forwarded to your endpoint.

0

If you need to automatically compress/decompress data coming or going through the request one way to use is request-promise node module

 import rp = require("request-promise");
 const requestOptions: rp.Options = {
            uri: "my-endpoint",
            gzip: true,
            method: "GET",
            encoding: "UTF-8",
            headers: {
                Accept: "*/*",
                Authorization: `xxx--token`
            }
        };
 rp(requestOptions)
            .then((response: any) =>
            { // ..your logic to process the response.. }

gzip - if true, add an Accept-Encoding header to request compressed content encodings from the server (if not already present) and decode supported content encodings in the response. Note: Automatic decoding of the response content is performed on the body data returned through request (both through the request stream and passed to the callback function) but is not performed on the response stream (available from the response event) which is the unmodified http.IncomingMessage object which may contain compressed data

request-promise-options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.