JSPM

  • Created
  • Published
  • Downloads 14034
  • Score
    100M100P100Q139030F
  • License MIT

HTTP Request snippet generator for *most* languages

Package Exports

  • @readme/httpsnippet
  • @readme/httpsnippet/src/helpers/code-builder

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 (@readme/httpsnippet) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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: 'http://mockbin.com/request'
});

options

Type: object

Available options:

  • escapeQueryStrings (boolean): In the event of you supplying a source that alreay contains escaped query strings, this allows you to disable automatic of query strings and prevents 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: 'http://mockbin.com/request'
});

// 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: 'http://mockbin.com/request'
});

// 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:

  • This targets Node 10+
  • Does not ship with a CLI component
  • Adds a useObjectBody option to the node and javascript targets. This option is a boolean flag that causes the request body to be rendered as an object literal wrapped in JSON.stringify. If disabled, it falls back to the original behavior of a stringified object body. This flag defaults to disabled.
  • Contains a escapeQueryStrings option on the core library to disable escaping of query strings in URLs. Helpful if the HAR being supplied already has them escaped.

License

MIT © Kong