Socket
Socket
Sign inDemoInstall

httpx

Package Overview
Dependencies
2
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    httpx

http(s) module with power


Version published
Weekly downloads
14K
increased by1.87%
Maintainers
1
Install size
62.0 kB
Created
Weekly downloads
 

Readme

Source

httpx

http(s) module with power.

NPM version Node.js CI codecov npm download

Installation

npm install httpx --save

Usage

import * as httpx from 'httpx';

Request URL

const response = await httpx.request('http://www.baidu.com/');
const body = await httpx.read(response, 'utf-8');
console.log(body);

Request SSE URL

const response = await httpx.request('sse url');
for await (const event of httpx.readAsSSE(response)) {
  console.log(event);
}

API

httpx.request(url[, options])

It returns Promise<Response>.

Requests the url with options, then return the response.

  • url String | Object - The URL to request, either a String or a Object that return by url.parse.
  • options Object - Optional
    • method String - Request method, defaults to GET. Could be GET, POST, DELETE or PUT.
    • data String | Buffer | Readable - Manually set the content of payload.
    • headers Object - Request headers.
    • timeout Number - Request timeout in milliseconds. Defaults to 3000. When timeout happen, will return RequestTimeout.
    • agent http.Agent - HTTP/HTTPS Agent object. Set false if you does not use agent.
    • beforeRequest Function - Before request hook, you can change every thing here.
    • compression Boolean - Enable compression support. Tell server side responses compressed data

httpx.read(response[, encoding])

It returns Promise<Buffer | String>.

Consume the response and read all data from the response.

  • response Response - the Client response. Don't setEncoding() for the response.
  • encoding String - Optional. If specify the encoding, will return String. If not specify encoding, return the buffer.

httpx.readAsSSE(response)

It returns AsyncGenerator<Event, void, unknown>.

Consume the response data with async iterator.

  • response Response - the Client response. Don't setEncoding() for the response.

Using with http proxy

import { SocksProxyAgent } from 'socks-proxy-agent';
import * as httpx from 'httpx';

await httpx.request('http://www.baidu.com/', {
  // pass a http proxy agent
  agent: new SocksProxyAgent('socks://your_proxy_server:3001')
});

License

The MIT license

FAQs

Last updated on 01 Apr 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc