0
const spdy = require("http2");
const zlib = require("zlib");

let payload = "<<DATA TO SEND>>"

const client = spdy.connect("https://<< HOSTNAME >>");

let outdata = '';

const req = client.request({
    ":authority": "<< Hostname >>",
    ":method": "POST",
    ":scheme": "https",
    ":path":  "<< path >>",
    "accept-encoding": "gzip"
});

req.setEncoding('utf8');

req.on("data", chunk => {
    chunk = chunk.toString("utf-8");
    outdata += chunk;
});

req.on("end", () => {

    client.close();
    console.log(outdata);
    const data = JSON.parse(outdata);
    
    // Write to do >> 
});

req.write(payload);
req.end();

I'm trying to solve an encoding problem like gzip when receiving a response using the http2 module.

I consulted the following link, but this was not very helpful.

How to use request or http module to read gzip page into a string

https://nodejs.org/api/zlib.html#zlib_compressing_http_requests_and_responses

If anyone has any idea on this,it will be great.

1 Answers1

1
const spdy = require("http2");
const zlib = require("zlib");

let payload = "<<DATA TO SEND>>"

const client = spdy.connect("https://<< HOSTNAME >>");

let outdata = '';

const req = client.request({
    ":authority": "<< Hostname >>",
    ":method": "POST",
    ":scheme": "https",
    ":path":  "<< path >>",
    "accept-encoding": "gzip"
});

req.setEncoding('utf8');

let zip = zlib.createGunzip();

req.pipe(zip);

zip.on("data", chunk => {
    chunk = chunk.toString("utf-8");
    outdata += chunk;
});

zip.on("end", () => {

    client.close();
    console.log(outdata);
    const data = JSON.parse(outdata);
    
    // Write to do >> 
});

req.write(payload);
req.end();

This also requires you to put ClientHttp2Stream in the pipeline like the http module.