Package Exports
- fetch-har
- fetch-har/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 (fetch-har) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
fetch-har
Make a fetch request from a HAR definition.
Installation
npm install --save fetch-har
Usage
require('isomorphic-fetch');
const fetchHar = require('.');
// If executing from an environment that dodoesn't normally provide fetch()
// you'll need to polyfill some APIs in order to make `multipart/form-data`
// requests.
if (!globalThis.FormData) {
globalThis.Blob = require('formdata-node').Blob;
globalThis.File = require('formdata-node').File;
globalThis.FormData = require('formdata-node').FormData;
}
const har = {
log: {
entries: [
{
request: {
headers: [
{
name: 'Authorization',
value: 'Bearer api-key',
},
{
name: 'Content-Type',
value: 'application/json',
},
],
queryString: [
{ name: 'a', value: 1 },
{ name: 'b', value: 2 },
],
postData: {
mimeType: 'application/json',
text: '{"id":8,"category":{"id":6,"name":"name"},"name":"name"}',
},
method: 'POST',
url: 'http://httpbin.org/post',
},
},
],
},
};
fetchHar(har)
.then(res => res.json())
.then(console.log);
API
If you are executing fetch-har
in a browser environment that supports the FormData API then you don't need to do anything. If you arent, however, you'll need to polyfill it.
Unfortunately the most popular NPM package form-data ships with a non-spec compliant API, and for this we don't recommend you use it, as if you use fetch-har
to upload files it may not work.
We recommend either formdata-node or formdata-polyfill.
Options
userAgent
A custom User-Agent
header to apply to your request. Please note that browsers have their own handling for these headers in fetch()
calls so it may not work everywhere; it will always be sent in Node however.
await fetchHar(har, { userAgent: 'my-client/1.0' });
files
An optional object map you can supply to use for multipart/form-data
file uploads in leu of relying on if the HAR you have has data URLs. It supports Node file buffers and the File API.
await fetchHar(har, { files: {
'owlbert.png': await fs.readFile('./owlbert.png'),
'file.txt': document.querySelector('#some-file-input').files[0],
} });
If you don't supply this option fetch-har
will fallback to the data URL present within the supplied HAR. If no files
option is present, and no data URL (via param.value
) is present in the HAR, a fatal exception will be thrown.
multipartEncoder
❗ If you are using
fetch-har
in Node you may need this option!
If you are running fetch-har
within a Node environment and you're using node-fetch@2
, or another fetch
polyfill that does not support a spec-compliant FormData
API, you will need to specify an encoder that will transform your FormData
object into something that can be used with Request.body.
We recommend form-data-encoder.
const { FormDataEncoder } = require('form-data-encoder');
await fetchHar(har, { multipartEncoder: FormDataEncoder });
You do not, and shouldn't, need to use this option in browser environments.