Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
489 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...