You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.9 KiB

4 years ago
  1. httpx
  2. ===========
  3. http(s) module with power.
  4. [![NPM version][npm-image]][npm-url]
  5. [![build status][travis-image]][travis-url]
  6. [![Coverage Status](https://coveralls.io/repos/JacksonTian/httpx/badge.svg?branch=master&service=github)](https://coveralls.io/github/JacksonTian/httpx?branch=master)
  7. [![David deps][david-image]][david-url]
  8. [![npm download][download-image]][download-url]
  9. [npm-image]: https://img.shields.io/npm/v/httpx.svg?style=flat-square
  10. [npm-url]: https://npmjs.org/package/httpx
  11. [travis-image]: https://img.shields.io/travis/JacksonTian/httpx.svg?style=flat-square
  12. [travis-url]: https://travis-ci.org/JacksonTian/httpx
  13. [david-image]: https://img.shields.io/david/JacksonTian/httpx.svg?style=flat-square
  14. [david-url]: https://david-dm.org/JacksonTian/httpx
  15. [download-image]: https://img.shields.io/npm/dm/httpx.svg?style=flat-square
  16. [download-url]: https://npmjs.org/package/httpx
  17. ## Installation
  18. ```bash
  19. $ npm install httpx --save
  20. ```
  21. ## Usage
  22. ```js
  23. 'use strict';
  24. const httpx = require('httpx');
  25. httpx.request('http://www.baidu.com/').then((response) => {
  26. response.pipe(process.stdout);
  27. response.on('end', () => {
  28. process.stdout.write('\n');
  29. });
  30. }, (err) => {
  31. // on error
  32. });
  33. ```
  34. Or with `co`.
  35. ```js
  36. co(function* () {
  37. var response = yield httpx.request('http://www.baidu.com/');
  38. response.pipe(process.stdout);
  39. response.on('end', () => {
  40. process.stdout.write('\n');
  41. });
  42. });
  43. ```
  44. Or with `async/await`.
  45. ```js
  46. (async function () {
  47. var response = await httpx.request('http://www.baidu.com/');
  48. response.pipe(process.stdout);
  49. response.on('end', () => {
  50. process.stdout.write('\n');
  51. });
  52. })();
  53. ```
  54. ## API
  55. ### `httpx.request(url[, options])`
  56. - **url** String | Object - The URL to request, either a String or a Object that return by [url.parse](http://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost).
  57. - ***options*** Object - Optional
  58. - ***method*** String - Request method, defaults to `GET`. Could be `GET`, `POST`, `DELETE` or `PUT`.
  59. - ***data*** String | [Buffer](http://nodejs.org/api/buffer.html) | Readable - Manually set the content of payload.
  60. - ***headers*** Object - Request headers.
  61. - ***timeout*** Number - Request timeout in milliseconds. Defaults to 3000. When timeout happen, will return `RequestTimeout`.
  62. - ***agent*** [http.Agent](http://nodejs.org/api/http.html#http_class_http_agent) - HTTP/HTTPS Agent object.
  63. Set `false` if you does not use agent.
  64. - ***beforeRequest*** Function - Before request hook, you can change every thing here.
  65. - ***compression*** Boolean - Enable compression support. Tell server side responses compressed data
  66. ### `httpx.read(response[, encoding])`
  67. - **response** Response - the Client response. Don't setEncoding() for the response.
  68. - **encoding** String - Optional.
  69. ## License
  70. The MIT license