Skip to content

Send the request to the http2 server, get the response header, but fail to get the response body. #102

Closed
@masx200

Description

@masx200
 node -v
v14.0.0

package.json

{
    "type": "module",
    "name": "temp",
    "version": "1.0.0",
    "main": "index.js",
    "license": "MIT",
    "dependencies": {
        "fetch-h2": "^2.4.5"
        
    }
}

Send the request to the http2 server

import fetchh2 from "fetch-h2";
const fetch = fetchh2.fetch;
fetch("https://translate.google.cn", {
timeout: 10000,
    headers: {
        "user-agent":
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36",
    },
    method: "GET",
})
    .then((res) => {
        console.log(res);
        console.log("statusCode:", res.status);
        console.log("headers:", Object.fromEntries(res.headers));

        let pro = res.text();

        return pro;
    })
    .then((body) => {
        console.log("body length", body.length);
        console.log(`body:\n`, body);
    });

get the response header, but fail to get the response body

StreamResponse {
  _length: null,
  _used: false,
  bodyUsed: [Getter],
  _signal: undefined,
  _body: <ref *1> Gunzip {
    _writeState: Uint32Array(2) [ 0, 0 ],
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: [],
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      sync: false,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      destroyed: false,
      errored: false,
      closed: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: false,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: null
    },
    _events: [Object: null prototype] {
      prefinish: [Function: prefinish],
      end: [Function: onend],
      finish: [Function: onfinish],
      error: [Function: onerror],
      close: [Function: onclose]
    },
    _eventsCount: 5,
    _maxListeners: undefined,
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: false,
      ending: true,
      ended: true,
      finished: true,
      destroyed: false,
      decodeStrings: true,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: true,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      afterWriteTickInfo: null,
      bufferedRequest: null,
      lastBufferedRequest: null,
      pendingcb: 0,
      prefinished: true,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      errored: false,
      closed: false,
      bufferedRequestCount: 0,
      corkedRequestsFree: [Object]
    },
    allowHalfOpen: true,
    _transformState: {
      afterTransform: [Function: bound afterTransform],
      needTransform: false,
      transforming: false,
      writecb: null,
      writechunk: null,
      writeencoding: null
    },
    bytesWritten: 0,
    _handle: Zlib {
      onerror: [Function: zlibOnError],
      buffer: <Buffer >,
      cb: [Function (anonymous)],
      availOutBefore: 16384,
      availInBefore: 0,
      inOff: 0,
      flushFlag: 2,
      [Symbol(owner)]: [Circular *1]
    },
    _outBuffer: <Buffer 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 48 40 00 00 00 00 00 00 e0 5f 4d 83 d9 01 00 00 30 60 4d 83 d9 01 00 00 40 60 4d 83 d9 01 00 00 58 60 ... 16334 more bytes>,
    _outOffset: 0,
    _chunkSize: 16384,
    _defaultFlushFlag: 2,
    _finishFlushFlag: 2,
    _defaultFullFlushFlag: 3,
    _info: undefined,
    _level: -1,
    _strategy: 0,
    [Symbol(kCapture)]: false,
    [Symbol(kError)]: null
  },
  _mime: 'text/html; charset=UTF-8',
  headers: GuardedHeaders [Map] {
    _guard: 'response',
    _data: Map(14) {
      'date' => [Array],
      'pragma' => [Array],
      'expires' => [Array],
      'cache-control' => [Array],
      'x-frame-options' => [Array],
      'content-type' => [Array],
      'content-language' => [Array],
      'p3p' => [Array],
      'x-content-type-options' => [Array],
      'content-encoding' => [Array],
      'server' => [Array],
      'content-length' => [Array],
      'x-xss-protection' => [Array],
      'alt-svc' => [Array]
    }
  },
  httpVersion: 2,
  ok: [Getter],
  redirected: false,
  status: 200,
  statusText: '',
  type: 'basic',
  url: 'https://translate.google.cn',
  useFinalURL: undefined
}
statusCode: 200
headers: {
  date: 'Sat, 25 Apr 2020 02:31:35 GMT',
  pragma: 'no-cache',
  expires: 'Fri, 01 Jan 1990 00:00:00 GMT',
  'cache-control': 'no-cache, must-revalidate',
  'x-frame-options': 'DENY',
  'content-type': 'text/html; charset=UTF-8',
  'content-language': 'zh-CN',
  p3p: 'CP="This is not a P3P policy! See g.co/p3phelp for more info."',
  'x-content-type-options': 'nosniff',
  'content-encoding': 'gzip',
  server: 'HTTP server (unknown)',
  'content-length': '55175',
  'x-xss-protection': '0',
  'alt-svc': 'quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,h3-T050=":443"; ma=2592000'
}
body length 0
body:

Use the native http2 module as follows. Successful response.

import { constants, connect } from "http2";

const {
    HTTP2_HEADER_STATUS,
    HTTP2_HEADER_PATH,
    HTTP2_HEADER_METHOD,
} = constants;
const client = connect("https://translate.google.cn", {});
client.on("error", (err) => console.error(err));

const req = client.request({
    [HTTP2_HEADER_PATH]: "/",
    [HTTP2_HEADER_METHOD]: "GET",
    "user-agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36",
});
console.log(req);
req.on("response", (headers, flags) => {
    console.log("statusCode:", headers[HTTP2_HEADER_STATUS]);
    console.log("headers:", headers);
});

req.setEncoding("utf8");
let data = [];
req.on("data", (chunk) => {
    data.push(chunk);
});
req.on("end", () => {
    let body = data.join("");
    console.log("body length", body.length);
    console.log(`body:\n`, body);
    client.close();
});
req.end();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions