@readme/httpsnippet

HTTP Request snippet generator for *most* languages

Usage no npm install needed!

<script type="module">
  import readmeHttpsnippet from 'https://cdn.skypack.dev/@readme/httpsnippet';
</script>

README

HTTP Snippet version License

HTTP Request snippet generator for many languages & tools including: cURL, HTTPie, Javascript, Node, C, Java, PHP, Objective-C, Swift, Python, Ruby, C#, Go, OCaml and more!

Relies on the popular HAR format to import data and describe HTTP calls.

See it in action on companion service: APIembed

Build

Installation

npm install --save @readme/httpsnippet

Usage

HTTPSnippet(source [, options])

source

Required Type: object

Name of conversion target

const HTTPSnippet = require('httpsnippet');

const snippet = new HTTPSnippet({
  method: 'GET',
  url: 'https://httpbin.com/anything'
});

options

Type: object

Available options:

  • harIsAlreadyEncoded (boolean): In the event of you supplying a source HAR that already contains escaped data (query and cookie parameters)strings, this allows you to disable automatic encoding of those parameters to prevent them from being double-escaped.

convert(target [, options])

target

Required Type: string

Name of conversion target

options

Type: object

Target options, see wiki for details

const HTTPSnippet = require('httpsnippet');

const snippet = new HTTPSnippet({
  method: 'GET',
  url: 'https://httpbin.com/anything'
});

// generate Node.js: Native output
console.log(snippet.convert('node'));

// generate Node.js: Native output, indent with tabs
console.log(snippet.convert('node', {
  indent: '\t'
}));

convert(target [, client, options])

target

Required Type: string

Name of conversion target

client

Type: string

Name of conversion target client library

options

Type: object

Target options, see wiki for details

const HTTPSnippet = require('httpsnippet');

const snippet = new HTTPSnippet({
  method: 'GET',
  url: 'https://httpbin.com/anything'
});

// generate Shell: cURL output
console.log(snippet.convert('shell', 'curl', {
  indent: '\t'
}));

// generate Node.js: Unirest output
console.log(snippet.convert('node', 'unirest'));

addTarget(target)

target

Required Type: object

Representation of a conversion target. Can use this to use targets that are not officially supported.

const customLanguageTarget = require('httpsnippet-for-my-lang');
HTTPSnippet.addTarget(customLanguageTarget);

addTargetClient(target, client)

target

Required Type: string

Name of conversion target

client

Required Type: object

Representation of a conversion target client. Can use this to use target clients that are not officially supported.

const customClient = require('httpsnippet-for-my-node-http-client');
HTTPSnippet.addTargetClient('node', customClient);

Documentation

At the heart of this module is the HAR Format as the HTTP request description format, please review some of the sample JSON HAR Request objects in test fixtures, or read the HAR Docs for more details.

For detailed information on each target, please review the wiki.

Differences from kong/httpsnippet

The main difference between this library and the upstream httpsnippet library are:

  • Does not ship with a CLI component.
  • The fetch target for Node and JS both treat body payloads as an object literal and wrap it within JSON.stringify(). We do this to keep those targets looking nicer with those kinds of payloads.
  • Contains a harIsAlreadyEncoded option on the core library to disable escaping of cookies and query strings in URLs. Helpful if the HAR being supplied already has them escaped.
  • PHP Guzzle snippets come with require_once('vendor/autoload.php'); at the top of them.
  • A full integration suite for testing out snippets the library creates.

Running the integration suite

docker-compose run integration_node
docker-compose run integration_php
docker-compose run integration_python
docker-compose run integration_shell

License

MIT © Kong