{"id":13581498,"url":"https://github.com/Kong/httpsnippet","last_synced_at":"2025-04-06T10:32:33.449Z","repository":{"id":26867328,"uuid":"30327615","full_name":"Kong/httpsnippet","owner":"Kong","description":"HTTP Request snippet generator for many languages \u0026 libraries","archived":false,"fork":false,"pushed_at":"2024-10-16T08:43:18.000Z","size":1299,"stargazers_count":1149,"open_issues_count":28,"forks_count":227,"subscribers_count":33,"default_branch":"master","last_synced_at":"2024-10-29T15:04:45.460Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-02-04T23:44:13.000Z","updated_at":"2024-10-19T15:58:24.000Z","dependencies_parsed_at":"2024-01-14T21:16:14.426Z","dependency_job_id":"4e60c27f-9966-4d86-8a79-fbb64947b85d","html_url":"https://github.com/Kong/httpsnippet","commit_stats":{"total_commits":492,"total_committers":65,"mean_commits":7.569230769230769,"dds":0.6280487804878049,"last_synced_commit":"5b4474df44564aae889423ab26e80d41f99034df"},"previous_names":["mashape/httpsnippet","ahmadnassri/httpsnippet"],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kong%2Fhttpsnippet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kong%2Fhttpsnippet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kong%2Fhttpsnippet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kong%2Fhttpsnippet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kong","download_url":"https://codeload.github.com/Kong/httpsnippet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243732296,"owners_count":20338837,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest"],"created_at":"2024-08-01T15:02:03.577Z","updated_at":"2025-04-06T10:32:33.423Z","avatar_url":"https://github.com/Kong.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","JavaScript","hacktoberfest"],"sub_categories":[],"readme":"# HTTPSnippet\n\n[![version][npm-version]][npm-url] [![License][npm-license]][license-url]\n\n\u003e HTTP Request snippet generator for _many_ languages \u0026 tools including: `cURL`, `HTTPie`, `JavaScript`, `Node`, `C`, `Java`, `PHP`, `Objective-C`, `Swift`, `Python`, `Ruby`, `C#`, `Go`, `OCaml`, `Crystal` and [more](https://github.com/Kong/httpsnippet/wiki/Targets)!\n\nRelies on the popular [HAR](http://www.softwareishard.com/blog/har-12-spec/#request) format to import data and describe HTTP calls.\n\nSee it in action on companion service: [APIembed](https://apiembed.com)\n\n[![Build](https://github.com/Kong/httpsnippet/actions/workflows/build.yml/badge.svg)](https://github.com/Kong/httpsnippet/actions/workflows/build.yml) [![Downloads][npm-downloads]][npm-url]\n\n- [HTTPSnippet](#httpsnippet)\n  - [Quickstart](#quickstart)\n    - [Core Concepts](#core-concepts)\n    - [CLI Quickstart](#cli-quickstart)\n    - [TypeScript Library Quickstart](#typescript-library-quickstart)\n  - [CLI Usage](#cli-usage)\n    - [CLI Installation](#cli-installation)\n    - [Example](#example)\n  - [TypeScript Library Usage](#typescript-library-usage)\n    - [Library Installation](#library-installation)\n    - [Types](#types)\n      - [`HarRequest`](#harrequest)\n      - [`HarEntry`](#harentry)\n      - [`TargetId`](#targetid)\n      - [`ClientId`](#clientid)\n      - [`Converter`](#converter)\n      - [`Client`](#client)\n      - [`ClientInfo`](#clientinfo)\n      - [`Extension`](#extension)\n      - [`TargetInfo`](#targetinfo)\n      - [`Target`](#target)\n    - [Library Exports](#library-exports)\n      - [`new HTTPSnippet(source: HarRequest | HarEntry)`](#new-httpsnippetsource-harrequest--harentry)\n      - [`snippet.convert(targetId: string, clientId?: string, options?: T)`](#snippetconverttargetid-string-clientid-string-options-t)\n      - [`isTarget`](#istarget)\n      - [`addTarget`](#addtarget)\n      - [`isClient`](#isclient)\n      - [`addTargetClient`](#addtargetclient)\n  - [Bugs and feature requests](#bugs-and-feature-requests)\n  - [Contributing](#contributing)\n\n## Quickstart\n\n### Core Concepts\n\n1. HTTPSnippet's input is a JSON object that represents an HTTP request in the [HAR Request Object format](http://www.softwareishard.com/blog/har-12-spec).\n1. HTTPSnippet's output is executable code that sends the input HTTP request, in a wide variety of languages and libraries.\n1. You provide HTTPSnippet your desired `target`, `client`, and `options`.\n   - a `target` refers to a group of code generators. Generally, a target is a _programming language_ like `Rust`, `Go`, `C`, or `OCaml`.\n   - `client` refers to a more specific generator within the parent target. For example, the `C#` target has two available clients, `httpclient` and `restsharp`, each referring to a popular C# library for making requests.\n   - `options` are per client and generally control things like specific indent behaviors or other formatting rules.\n\n### CLI Quickstart\n\n```shell\nhttpsnippet har.json \\ # the path your input file (must be in HAR format)\n  --target shell \\ # your desired language\n  --client curl \\ # your desired language library\n  --output ./examples \\ # an output directory, otherwise will just output to Stdout\n  --options '{ \"indent\": false }' # any client options as a JSON string\n```\n\n### TypeScript Library Quickstart\n\n```ts\nimport { HTTPSnippet } from 'httpsnippet';\n\nconst snippet = new HTTPSnippet({\n  method: 'GET',\n  url: 'http://mockbin.com/request',\n});\n\nconst options = { indent: '\\t' };\nconst output = snippet.convert('shell', 'curl', options);\nconsole.log(output);\n```\n\n## CLI Usage\n\n### CLI Installation\n\n| NPM                                         | Yarn                                   |\n| ------------------------------------------- | -------------------------------------- |\n| \u003cpre\u003enpm install --global httpsnippet\u003c/pre\u003e | \u003cpre\u003eyarn global add httpsnippet\u003c/pre\u003e |\n\n```text\nhttpsnippet [harFilePath]\n\nthe default command\n\nOptions:\n      --help     Show help                                   [boolean]\n      --version  Show version number                         [boolean]\n  -t, --target   target output                     [string] [required]\n  -c, --client   language client                              [string]\n  -o, --output   write output to directory                    [string]\n  -x, --options  provide extra options for the target/client  [string]\n\nExamples:\n  httpsnippet my_har.json --target rust --client actix --output my_src_directory\n```\n\n### Example\n\nThe input to HTTPSnippet is any valid [HAR Request Object](http://www.softwareishard.com/blog/har-12-spec/#request), or full [HAR](http://www.softwareishard.com/blog/har-12-spec/#log) log format.\n\n\u003cdetails\u003e\n\u003csummary\u003e`example.json`\u003c/summary\u003e\n\n```json\n{\n  \"method\": \"POST\",\n  \"url\": \"http://mockbin.com/har?key=value\",\n  \"httpVersion\": \"HTTP/1.1\",\n  \"queryString\": [\n    {\n      \"name\": \"foo\",\n      \"value\": \"bar\"\n    },\n    {\n      \"name\": \"foo\",\n      \"value\": \"baz\"\n    },\n    {\n      \"name\": \"baz\",\n      \"value\": \"abc\"\n    }\n  ],\n  \"headers\": [\n    {\n      \"name\": \"accept\",\n      \"value\": \"application/json\"\n    },\n    {\n      \"name\": \"content-type\",\n      \"value\": \"application/x-www-form-urlencoded\"\n    }\n  ],\n  \"cookies\": [\n    {\n      \"name\": \"foo\",\n      \"value\": \"bar\"\n    },\n    {\n      \"name\": \"bar\",\n      \"value\": \"baz\"\n    }\n  ],\n  \"postData\": {\n    \"mimeType\": \"application/x-www-form-urlencoded\",\n    \"params\": [\n      {\n        \"name\": \"foo\",\n        \"value\": \"bar\"\n      }\n    ]\n  }\n}\n```\n\n\u003c/details\u003e\n\n```shell\nhttpsnippet example.json --target shell --client curl --output ./examples\n```\n\n```console\n$ tree examples\nexamples/\n└── example.sh\n```\n\ninside `examples/example.sh` you'll see the generated output:\n\n```shell\ncurl --request POST \\\n  --url 'http://mockbin.com/har?foo=bar\u0026foo=baz\u0026baz=abc\u0026key=value' \\\n  --header 'accept: application/json' \\\n  --header 'content-type: application/x-www-form-urlencoded' \\\n  --cookie 'foo=bar; bar=baz' \\\n  --data foo=bar\n```\n\nprovide extra options:\n\n```shell\nhttpsnippet example.json --target shell --client curl --output ./examples --options '{ \"indent\": false }'\n```\n\nand see how the output changes, in this case without indentation\n\n```shell\ncurl --request POST --url 'http://mockbin.com/har?foo=bar\u0026foo=baz\u0026baz=abc\u0026key=value' --header 'accept: application/json' --header 'content-type: application/x-www-form-urlencoded' --cookie 'foo=bar; bar=baz' --data foo=bar\n```\n\n## TypeScript Library Usage\n\n### Library Installation\n\n| NPM                                       | Yarn                            |\n| ----------------------------------------- | ------------------------------- |\n| \u003cpre\u003enpm install --save httpsnippet\u003c/pre\u003e | \u003cpre\u003eyarn add httpsnippet\u003c/pre\u003e |\n\n### Types\n\n#### `HarRequest`\n\nSee \u003chttps://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/har-format\u003e for the TypeScript type corresponding to this type\n\n#### `HarEntry`\n\n```ts\ninterface Entry {\n  request: Partial\u003cHarRequest\u003e;\n}\n\ninterface HarEntry {\n  log: {\n    version: string;\n    creator: {\n      name: string;\n      version: string;\n    };\n    entries: {\n      request: Partial\u003cHarRequest\u003e;\n    }[];\n  };\n}\n```\n\n#### `TargetId`\n\n```ts\ntype TargetId = string;\n```\n\n#### `ClientId`\n\n```ts\ntype ClientId = string;\n```\n\n#### `Converter`\n\n```ts\ntype Converter\u003cT extends Record\u003cstring, any\u003e\u003e = (\n  request: Request,\n  options?: Merge\u003cCodeBuilderOptions, T\u003e,\n) =\u003e string;\n```\n\n#### `Client`\n\n```ts\ninterface Client\u003cT extends Record\u003cstring, any\u003e = Record\u003cstring, any\u003e\u003e {\n  info: ClientInfo;\n  convert: Converter\u003cT\u003e;\n}\n```\n\n#### `ClientInfo`\n\n```ts\ninterface ClientInfo {\n  key: ClientId;\n  title: string;\n  link: string;\n  description: string;\n}\n```\n\n#### `Extension`\n\n```ts\ntype Extension = `.${string}` | null;\n```\n\n#### `TargetInfo`\n\n```ts\ninterface TargetInfo {\n  key: TargetId;\n  title: string;\n  extname: Extension;\n  default: string;\n}\n```\n\n#### `Target`\n\n```ts\ninterface Target {\n  info: TargetInfo;\n  clientsById: Record\u003cClientId, Client\u003e;\n}\n```\n\n### Library Exports\n\n#### `new HTTPSnippet(source: HarRequest | HarEntry)`\n\nName of [conversion target](https://github.com/Kong/httpsnippet/wiki/Targets)\n\n```ts\nimport { HTTPSnippet } from 'httpsnippet';\n\nconst snippet = new HTTPSnippet({\n  method: 'GET',\n  url: 'http://mockbin.com/request',\n});\n```\n\n#### `snippet.convert(targetId: string, clientId?: string, options?: T)`\n\nThe `convert` method requires a target ID such as `node`, `shell`, `go`, etc. If no client ID is provided, the default client for that target will be used.\n\n\u003e Note: to see the default targets for a given client, see `target.info.default`. For example [`shell`'s](src/targets/shell/target.ts) target has the default of `curl`.\n\nMany targets provide specific options. Look at the TypeScript types for the target you are interested in to see what options it provides. For example `shell:curl`'s options correspond to the `CurlOptions` interface in [the `shell:curl` client file](src/targets/shell/curl/client.ts).\n\n```ts\nimport { HTTPSnippet } from 'httpsnippet';\n\nconst snippet = new HTTPSnippet({\n  method: 'GET',\n  url: 'http://mockbin.com/request',\n});\n\n// generate Node.js: Native output\nconsole.log(snippet.convert('node'));\n\n// generate Node.js: Native output, indent with tabs\nconsole.log(\n  snippet.convert('node', {\n    indent: '\\t',\n  }),\n);\n```\n\n#### `isTarget`\n\nUseful for validating that a custom target is considered valid by HTTPSnippet.\n\n```ts\nconst isTarget: (target: Target) =\u003e target is Target;\n```\n\n```ts\nimport { myCustomTarget } from './my-custom-target';\nimport { isTarget } from 'httpsnippet';\n\ntry {\n  console.log(isTarget(myCustomTarget));\n} catch (error) {\n  console.error(error);\n}\n```\n\n#### `addTarget`\n\nUse `addTarget` to add a new custom target that you can then use in your project.\n\n```ts\nconst addTarget: (target: Target) =\u003e void;\n```\n\n```ts\nimport { myCustomClient } from './my-custom-client';\nimport { HAR } from 'my-custom-har';\nimport { HTTPSnippet, addTargetClient } from 'httpsnippet';\n\naddTargetClient(myCustomClient);\n\nconst snippet = new HTTPSnippet(HAR);\nconst output = snippet.convert('customTargetId');\nconsole.log(output);\n```\n\n#### `isClient`\n\nUseful for validating that a custom client is considered valid by HTTPSnippet.\n\n```ts\nconst isClient: (client: Client) =\u003e client is Client;\n```\n\n```ts\nimport { myCustomClient } from './my-custom-client';\nimport { isClient } from 'httpsnippet';\n\ntry {\n  console.log(isClient(myCustomClient));\n} catch (error) {\n  console.error(error);\n}\n```\n\n#### `addTargetClient`\n\nUse `addTargetClient` to add a custom client to an existing target. See [`addTarget`](#addtarget) for how to add a custom target.\n\n```ts\nconst addTargetClient: (targetId: TargetId, client: Client) =\u003e void;\n```\n\n```ts\nimport { myCustomClient } from './my-custom-client';\nimport { HAR } from 'my-custom-har';\nimport { HTTPSnippet, addTargetClient } from 'httpsnippet';\n\naddTargetClient('customTargetId', myCustomClient);\n\nconst snippet = new HTTPSnippet(HAR);\nconst output = snippet.convert('customTargetId', 'customClientId');\nconsole.log(output);\n```\n\n## Bugs and feature requests\n\nHave a bug or a feature request? Please first read the [issue guidelines](CONTRIBUTING.md#using-the-issue-tracker) and search for existing and closed issues. If your problem or idea is not addressed yet, [please open a new issue](/issues).\n\n## Contributing\n\nPlease read through our [contributing guidelines](CONTRIBUTING.md). Included are directions for opening issues, coding standards, and notes on development.\n\nFor info on creating new conversion targets, please review this [guideline](https://github.com/Kong/httpsnippet/wiki/Creating-Targets)\n\nMoreover, if your pull request contains TypeScript patches or features, you must include relevant unit tests.\n\nEditor preferences are available in the [editor config](.editorconfig) for easy use in common text editors. Read more and download plugins at \u003chttp://editorconfig.org\u003e.\n\n[license-url]: https://github.com/Kong/httpsnippet/blob/master/LICENSE\n[npm-url]: https://www.npmjs.com/package/httpsnippet\n[npm-license]: https://img.shields.io/npm/l/httpsnippet.svg?style=flat-square\n[npm-version]: https://img.shields.io/npm/v/httpsnippet.svg?style=flat-square\n[npm-downloads]: https://img.shields.io/npm/dm/httpsnippet.svg?style=flat-square\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKong%2Fhttpsnippet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKong%2Fhttpsnippet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKong%2Fhttpsnippet/lists"}