Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fetch-har

Package Overview
Dependencies
Maintainers
10
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-har

Make a fetch request from a HAR definition

  • 8.1.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
10
Created
Source

fetch-har

Make a fetch request from a HAR definition.

CI License

Features

  • Supports Node 14+ (including the native fetch implementation in Node 18!).
  • Natively works in all browsers that support fetch without having to use any polyfils.
  • Tested across Chrome, Safari, Firefox on Mac, Windows, and Linux.
  • Requests can be mocked with nock or fetch-mock.

Installation

npm install --save fetch-har

Usage

require('isomorphic-fetch');

// If executing from an environment that doesn't normally provide `fetch()` we'll automatically
// polyfill in the `Blob`, `File`, and `FormData` APIs with the optional `formdata-node` package
// (provided you've installed it).
const fetchHAR = require('fetch-har').default;
// import fetchHAR from 'fetch-har'); // Or if you're in an ESM codebase.

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.

Though we recommend either formdata-node or formdata-polyfill we prefer formdata-node right now as it's CJS-compatible.

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 to execute multipart/form-data requests!

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.

init

This optional argument lets you supply any option that's available to supply to the Request constructor.

await fetchHAR(har, {
  init: {
    headers: new Headers({
      'x-custom-header': 'buster',
    }),
  },
})

❗ Note that if you supply body or credentials to this option they may be overridden by what your HAR requires.

FAQs

Package last updated on 29 Jul 2022

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