|
1 | | -# HTTP Snippet |
| 1 | +# HTTP Snippet [![version][npm-version]][npm-url] [![License][npm-license]][license-url] |
| 2 | + |
| 3 | +HTTP Request snippet generator for *most* languages. |
| 4 | +Relies on the popular [HAR](http://www.softwareishard.com/blog/har-12-spec/#request) format to import data and describe HTTP calls. |
| 5 | + |
| 6 | +[![Build Status][travis-image]][travis-url] |
| 7 | +[![Downloads][npm-downloads]][npm-url] |
| 8 | +[![Code Climate][codeclimate-quality]][codeclimate-url] |
| 9 | +[![Coverage Status][codeclimate-coverage]][codeclimate-url] |
| 10 | +[![Dependencies][david-image]][david-url] |
| 11 | + |
| 12 | +## Table of contents |
| 13 | +- [Targets](#Targets) |
| 14 | +- [Installation](#installation) |
| 15 | +- [Usage](#usage) |
| 16 | +- [Documentation](#documentation) |
| 17 | +- [Bugs and feature requests](#bugs-and-feature-requests) |
| 18 | +- [Contributing](#contributing) |
| 19 | +- [Versioning](#versioning) |
| 20 | +- [License](#license) |
| 21 | + |
| 22 | +## Targets |
| 23 | + |
| 24 | +currently the following output [targets](/src/targets) are supported: |
| 25 | + |
| 26 | +- [cURL](http://curl.haxx.se/) |
| 27 | +- [HTTPie](http://httpie.org) |
| 28 | +- Node.js |
| 29 | + - [Native](http://nodejs.org/api/http.html#http_http_request_options_callback) |
| 30 | +- PHP |
| 31 | + - [ext-curl](http://php.net/manual/en/book.curl.php) |
| 32 | +- [Wget](https://www.gnu.org/software/wget/) |
| 33 | + |
| 34 | +## Installation |
| 35 | + |
| 36 | +install from source or through [npm](https://www.npmjs.com/): |
2 | 37 |
|
3 | 38 | ```shell |
4 | | -npm install -g httpsnippet |
| 39 | +# to use in cli |
| 40 | +npm install --global httpsnippet |
| 41 | + |
| 42 | +# to use as a module |
| 43 | +npm install httpsnippet |
5 | 44 | ``` |
6 | 45 |
|
7 | 46 | ## Usage |
8 | 47 |
|
| 48 | +### CLI |
| 49 | + |
| 50 | +``` |
| 51 | +Usage: httpsnippet [options] <file> |
| 52 | +
|
| 53 | +Options: |
| 54 | +
|
| 55 | + -h, --help output usage information |
| 56 | + -V, --version output the version number |
| 57 | + -t, --target <target> target output |
| 58 | + -o, --output <directory> write output to directory |
| 59 | + -n, --output-name <name> output file name |
| 60 | +``` |
| 61 | + |
| 62 | +#### Example |
| 63 | + |
| 64 | +process single file: |
| 65 | + |
| 66 | +```shell |
| 67 | +httpsnippet my-api-endpoint.json --langauge php --output ./snippets |
| 68 | +``` |
| 69 | + |
9 | 70 | ```shell |
10 | | -httpsnippet har.json --langauge PHP --output snippet.php |
| 71 | +tree snippets |
| 72 | +``` |
| 73 | + |
| 74 | +``` |
| 75 | +snippets/ |
| 76 | +└── my-api-endpoint.php |
| 77 | +``` |
| 78 | + |
| 79 | +process multiple files: |
| 80 | + |
| 81 | +```shell |
| 82 | +httpsnippet /*.json --langauge nodejs --output ./snippets |
| 83 | +``` |
| 84 | + |
| 85 | +```shell |
| 86 | +tree sources/ |
| 87 | +``` |
| 88 | + |
| 89 | +``` |
| 90 | +sources/ |
| 91 | +├── endpoint-1.json |
| 92 | +├── endpoint-2.json |
| 93 | +└── endpoint-3.json |
| 94 | +``` |
| 95 | + |
| 96 | +```shell |
| 97 | +tree snippets/ |
| 98 | +``` |
| 99 | + |
| 100 | +``` |
| 101 | +snippets/ |
| 102 | +├── endpoint-1.js |
| 103 | +├── endpoint-2.js |
| 104 | +└── endpoint-3.js |
11 | 105 | ``` |
12 | 106 |
|
13 | | -### Options |
14 | 107 |
|
15 | | -| Name | Description | |
16 | | -| ------------ | ----------------------------------------- | |
17 | | -| `--langauge` | | |
18 | | -| `--output` | | |
| 108 | +### Module |
| 109 | + |
| 110 | +```js |
| 111 | +var httpsnippet = require('httpsnippet'); |
| 112 | +var snippet = new httpsnippet(source); |
| 113 | + |
| 114 | +// generate cURL output |
| 115 | +console.log(snippet.curl({ |
| 116 | + indent: ' '; |
| 117 | +})); |
| 118 | + |
| 119 | +// generate nodeJS output |
| 120 | +snippet.nodejs(); |
| 121 | +``` |
| 122 | + |
| 123 | +## Documentation |
| 124 | + |
| 125 | +At the heart of this module is the [HAR Request object](http://www.softwareishard.com/blog/har-12-spec/#request) as the http request description format, please review some of the sample JSON HAR Request objects in [test fixtures](/test/fixtures), or read the [HAR Docs](http://www.softwareishard.com/blog/har-12-spec/#request) for more details. |
| 126 | + |
| 127 | +### Output Targets |
| 128 | + |
| 129 | +output [targets](/src/targets) are simple modules that expose a constructor *(which handles the transformation)* and a utility method `extname` *(used in CLI for writing output into disk)* |
| 130 | + |
| 131 | +```js |
| 132 | +module.exports = function (opts) { |
| 133 | + // optionally process `opts` object for target specific configuration |
| 134 | + // |
| 135 | + // process `this.source` object |
| 136 | + // |
| 137 | + // return processed output as string |
| 138 | +}; |
| 139 | + |
| 140 | +module.exports.extname = function () { |
| 141 | + // return preferred filename extention |
| 142 | +}; |
| 143 | +``` |
| 144 | + |
| 145 | +### Target Options |
| 146 | + |
| 147 | +### cURL |
| 148 | + |
| 149 | +| Option | Default | Description | |
| 150 | +| -------- | ------- | ------------------------------------------------------------------------ | |
| 151 | +| `short` | `false` | use short form of cURL CLI options | |
| 152 | +| `indent` | ` ` | line break & indent output value, set to `false` to disable line breaks | |
| 153 | + |
| 154 | +### HTTPie |
| 155 | + |
| 156 | +| Option | Default | Description | |
| 157 | +| --------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | |
| 158 | +| `body` | `false` | only the response body is printed | |
| 159 | +| `headers` | `false` | only the response headers are printed | |
| 160 | +| `verbose` | `false` | print the whole HTTP exchange (request and response) | |
| 161 | +| `print` | `false` | selects parts of the HTTP exchange, e.g. `--print=Hh` *(see [httpie docs](https://github.com/jakubroztocil/httpie#output-options))* | |
| 162 | +| `cert` | `false` | use a client side certificate *(see [httpie docs](https://github.com/jakubroztocil/httpie#client-side-ssl-certificate))* | |
| 163 | +| `verify` | `false` | server SSL certificate verification *(see [httpie docs](https://github.com/jakubroztocil/httpie#server-ssl-certificate-verification))* | |
| 164 | +| `pretty` | `false` | syntax highlighting *(see [httpie docs](https://github.com/jakubroztocil/httpie#colors-and-formatting))* | |
| 165 | +| `style` | `false` | syntax highlighting *(see [httpie docs](https://github.com/jakubroztocil/httpie#colors-and-formatting))* | |
| 166 | +| `timeout` | `false` | overwrite the default *30s* timeout | |
| 167 | +| `short` | `false` | use short form of cURL CLI options | |
| 168 | +| `indent` | ` ` | line break & indent output value, set to `false` to disable line breaks | |
| 169 | + |
| 170 | +### Wget |
| 171 | + |
| 172 | +| Option | Default | Description | |
| 173 | +| --------- | ------- | ------------------------------------------------------------------------ | |
| 174 | +| `short` | `false` | use short form of cURL CLI options | |
| 175 | +| `indent` | ` ` | line break & indent output value, set to `false` to disable line breaks | |
| 176 | +| `verbose` | `false` | by default, `--quiet` is always used, unless `verbose` is set to `true` | |
| 177 | + |
| 178 | +## Bugs and feature requests |
| 179 | + |
| 180 | +Have 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). |
| 181 | + |
| 182 | +## Contributing |
| 183 | + |
| 184 | +Please read through our [contributing guidelines](CONTRIBUTING.md). Included are directions for opening issues, coding standards, and notes on development. |
| 185 | + |
| 186 | +More over, if your pull request contains JavaScript patches or features, you must include relevant unit tests. |
| 187 | + |
| 188 | +Editor preferences are available in the [editor config](.editorconfig) for easy use in common text editors. Read more and download plugins at <http://editorconfig.org>. |
| 189 | + |
| 190 | +## Versioning |
| 191 | + |
| 192 | +For transparency into our release cycle and in striving to maintain backward compatibility, this project is maintained under the Semantic Versioning guidelines. Sometimes we screw up, but we'll adhere to these rules whenever possible. |
| 193 | + |
| 194 | +Releases will be numbered with the following format: |
| 195 | + |
| 196 | +`<major>.<minor>.<patch>` |
| 197 | + |
| 198 | +And constructed with the following guidelines: |
| 199 | + |
| 200 | +- Breaking backward compatibility **bumps the major** while resetting minor and patch |
| 201 | +- New additions without breaking backward compatibility **bumps the minor** while resetting the patch |
| 202 | +- Bug fixes and misc changes **bumps only the patch** |
| 203 | + |
| 204 | +For more information on SemVer, please visit <http://semver.org/>. |
| 205 | + |
| 206 | +## License |
| 207 | + |
| 208 | +Licensed under [The MIT License](LICENSE). |
| 209 | + |
| 210 | +---- |
| 211 | + |
| 212 | +[license-url]: https://github.com/ahmadnassri/httpsnippet/blob/master/LICENSE |
| 213 | + |
| 214 | +[travis-url]: https://travis-ci.org/ahmadnassri/httpsnippet |
| 215 | +[travis-image]: https://img.shields.io/travis/ahmadnassri/httpsnippet.svg?style=flat-square |
| 216 | + |
| 217 | +[npm-url]: https://www.npmjs.com/package/httpsnippet |
| 218 | +[npm-license]: https://img.shields.io/npm/l/httpsnippet.svg?style=flat-square |
| 219 | +[npm-version]: https://img.shields.io/npm/v/httpsnippet.svg?style=flat-square |
| 220 | +[npm-downloads]: https://img.shields.io/npm/dm/httpsnippet.svg?style=flat-square |
| 221 | + |
| 222 | +[codeclimate-url]: https://codeclimate.com/github/ahmadnassri/httpsnippet |
| 223 | +[codeclimate-quality]: https://img.shields.io/codeclimate/github/ahmadnassri/httpsnippet.svg?style=flat-square |
| 224 | +[codeclimate-coverage]: https://img.shields.io/codeclimate/coverage/github/ahmadnassri/httpsnippet.svg?style=flat-square |
| 225 | + |
| 226 | +[david-url]: https://david-dm.org/ahmadnassri/httpsnippet |
| 227 | +[david-image]: https://img.shields.io/david/ahmadnassri/httpsnippet.svg?style=flat-square |
0 commit comments