Package Exports
- httpx
- httpx/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (httpx) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
httpx
http(s) module with power.
Installation
npm install httpx --saveUsage
Request URL
(async function () {
const response = await httpx.request('http://www.baidu.com/');
const body = await httpx.read(response, 'utf-8');
console.log(body);
})();Request SSE URL
(async function () {
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 beGET,POST,DELETEorPUT. - 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
falseif 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
- method String - Request method, defaults to
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
const { SocksProxyAgent } = require('socks-proxy-agent');
const httpx = require('httpx');
httpx.request('http://www.baidu.com/', {
// pass a http proxy agent
agent: new SocksProxyAgent('socks://your_proxy_server:3001')
});License
The MIT license