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)

Geek
  • 481
  • 5
  • 20
  • 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 '19 at 06:12
  • I want to use content encoding from client. I don't want to gzip it on server. – Geek Oct 30 '19 at 07:02
  • Show what have you done so far? – Sohan Oct 30 '19 at 07:21
  • One way is to do using requestPromise https://github.com/request/request#requestoptions-callback. See gzip option. If you think this can be helpful I can provide more detail – Sohan Oct 30 '19 at 08: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 '19 at 08:21
  • @geek I have tried answering the question. This is what I think can be done – Sohan Oct 30 '19 at 08:30
  • Possible duplicate of https://stackoverflow.com/questions/13031968/compressing-http-post-data-sent-from-browser – bhavesh27 Nov 01 '19 at 17:08

2 Answers2

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.

Plus Pingya
  • 171
  • 8
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

Sohan
  • 6,252
  • 5
  • 35
  • 56