Functional HTTP client for Node.js and Browsers w/ async/await.
Incredibly small browser version built on fetch w/ no external dependencies or polyfills.
const bent =const getJSON =const getBuffer =let obj = awaitlet buffer = await
As you can see, bent is a function that returns an async function.
Bent takes options which constrain what is accepted by the client. Any response that falls outside the constraints will generate an error.
You can provide these options in any order, and Bent will figure out which option is which by inspecting the option's type and content.
const post = ;const response = await ;
If you don't set a response encoding (
then the native response object will be returned after the statusCode check.
const bent =const getStream =let stream = await
The following options are available.
- HTTP Method:
'PUT', or any other ALLCAPS string will be used to set the HTTP method. Defaults to
- Response Format: Available formats are
'json'. By default, the response object/stream will be returned instead of a decoded response. Browser returns
- Status Codes: Any number will be considered an acceptable status code.
200is the only acceptable status code. When any status codes are provided,
200must be included explicitly in order to be acceptable.
- Headers: An object can be passed to set request headers.
- Base URL: Any string that begins with 'https:' or 'http:' is considered the Base URL. Subsequent queries need only pass the remaining URL string.
The returned async function is used for subsequent requests.
When working with Binary this library uses different types in the browser and Node.js. In Node.js all binary must be done
Buffer type. In the browser you can use ArrayBuffer or any ArrayBuffer view type (UInt8Array, etc).
async request(url[, body=null])
- url: Fully qualified URL to the remote resource, or in the case that a base URL is passed the remaining URL string.
- body: Request body. Can be a string, a stream (node.js), a buffer (node.js), an ArrayBuffer (browser), or a JSON object.
const bent =const put =await