New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

node-fetch-h2

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-fetch-h2

Implementation of window.fetch which can use http2 seamlessly

  • 2.3.1-0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is node-fetch-h2?

The node-fetch-h2 package is an HTTP/2 client for Node.js, which extends the popular node-fetch package to support HTTP/2. It allows you to make HTTP/2 requests with a similar API to node-fetch, making it easy to transition from HTTP/1.1 to HTTP/2.

What are node-fetch-h2's main functionalities?

Basic HTTP/2 GET Request

This feature allows you to make a basic HTTP/2 GET request to a specified URL and log the response text.

const fetch = require('node-fetch-h2');

async function fetchData() {
  const response = await fetch('https://example.com');
  const data = await response.text();
  console.log(data);
}

fetchData();

HTTP/2 POST Request with JSON Body

This feature allows you to make an HTTP/2 POST request with a JSON body to a specified URL and log the JSON response.

const fetch = require('node-fetch-h2');

async function postData() {
  const response = await fetch('https://example.com/api', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ key: 'value' })
  });
  const data = await response.json();
  console.log(data);
}

postData();

Handling HTTP/2 Headers

This feature allows you to make an HTTP/2 request with custom headers and log the response headers.

const fetch = require('node-fetch-h2');

async function fetchWithHeaders() {
  const response = await fetch('https://example.com', {
    headers: { 'Custom-Header': 'value' }
  });
  const headers = response.headers;
  headers.forEach((value, name) => console.log(`${name}: ${value}`));
}

fetchWithHeaders();

Streaming Response

This feature allows you to handle streaming responses from an HTTP/2 request, reading the response body in chunks.

const fetch = require('node-fetch-h2');

async function streamResponse() {
  const response = await fetch('https://example.com');
  const reader = response.body.getReader();
  const decoder = new TextDecoder('utf-8');
  let result = '';
  let done, value;
  while (!done) {
    ({ done, value } = await reader.read());
    result += decoder.decode(value, { stream: true });
  }
  console.log(result);
}

streamResponse();

Other packages similar to node-fetch-h2

Keywords

FAQs

Package last updated on 30 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc