{"id":21937634,"url":"https://github.com/cap32/http-ask","last_synced_at":"2026-04-17T08:06:16.605Z","repository":{"id":57268060,"uuid":"71765144","full_name":"Cap32/http-ask","owner":"Cap32","description":"A flexible promise based HTTP client for Node.js and browser.","archived":false,"fork":false,"pushed_at":"2017-12-11T07:26:26.000Z","size":133,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-27T13:48:28.446Z","etag":null,"topics":["ajax","http-client","promise"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Cap32.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-24T07:52:23.000Z","updated_at":"2017-01-12T09:51:27.000Z","dependencies_parsed_at":"2022-09-02T05:41:06.709Z","dependency_job_id":null,"html_url":"https://github.com/Cap32/http-ask","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fhttp-ask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fhttp-ask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fhttp-ask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cap32%2Fhttp-ask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cap32","download_url":"https://codeload.github.com/Cap32/http-ask/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244967889,"owners_count":20540029,"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":["ajax","http-client","promise"],"created_at":"2024-11-29T01:20:57.499Z","updated_at":"2026-04-17T08:06:16.533Z","avatar_url":"https://github.com/Cap32.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"A flexible promise based HTTP client for Node.js and browser.\n\n[![Build Status](https://travis-ci.org/Cap32/http-ask.svg?branch=master)](https://travis-ci.org/Cap32/http-ask) [![http-ask code style](https://img.shields.io/badge/code_style-http--ask-brightgreen.svg)](https://github.com/Cap32/http-ask) [![npm version](https://badge.fury.io/js/http-ask.svg)](https://badge.fury.io/js/http-ask)\n\n## Features\n\n- Cloneable and combinable request config\n- Support Node.js and browser\n- Promise/A+ based\n- Chainable API\n- Cancelable\n- Support timeout\n\n## Installing\n\nUsing npm:\n\n```bash\n$ npm install http-ask\n```\n\nUsing yarn:\n\n```bash\n$ yarn add http-ask\n```\n\n## Usage\n\nBasic `GET` request\n\n```js\n// Fetch a user with query (eg: http://localhost/api/users?page=32)\nAsk\n\t.create('http://localhost/api/users')\n\t.query({ page: 32 })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\n// Optionally, you can use an `ask` instance\nconst ask = new Ask('http://localhost/api/users');\nask\n\t.query({ page: 32 })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n```\n\nCombinable `url`\n\n```js\n// Fetch a user by id. (eg: http://localhost/api/users/2333)\nconst id = 2333;\n\nAsk\n\t.create(`http://localhost/api/users/${id}`)\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\n// Above could also be done as\nAsk\n\t.create('http://localhost')\n\t.url('api/users')\n\t.url(id)\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n```\n\nCombinable `query`\n\n```js\n// Fetch users with token and other query. (eg: http://localhost/api/users?token=asdf\u0026page=23\u0026count=10)\nconst token = 'asdf';\n\nAsk\n\t.create('http://localhost/api/users')\n\t.query({ token, page: 23, count: 10 })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\n// Above could also be done as\nAsk\n\t.create('/users')\n\t.query({ token })\n\t.query({ page: 23, count: 10 })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\n```\n\nClone `ask` instance\n\n```js\nconst apiHost = 'http://localhost/api';\nconst token = 'asdf';\n\n// create a common api `ask` instance\nconst askApiWithToken = new Ask(apiHost).query({ token });\n\naskApiWithToken\n\t.clone()\n\t.url('users')\n\t.query({ page: 23, count: 10 })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\naskApiWithToken\n\t.clone()\n\t.url('users')\n\t.query({ page: 1 })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n```\n\nPerforming `POST`, `PUT`, `DELETE` request\n\n```js\n\n// create a common posts `ask` instance\nconst askPosts = askApiWithToken.clone().url('posts');\n\n// post\naskPosts\n\t.clone()\n\t.post()\n\t.body({ name: 'Chirs' })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\n// put\naskPosts\n\t.clone()\n\t.put(id)\n\t.body({ name: 'Chirs' })\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\n// delete\naskPosts\n\t.clone()\n\n\t.method('delete')\n\t.url(id)\n\t// Above two lines are equal with `.delete(id)`\n\n\t.exec()\n\t.then((data) =\u003e console.log(data))\n\t.catch((error) =\u003e console.log(error))\n;\n\n```\n\n## API\n\n### Class: new Ask([url[, config]])\n\nCreate an ask instance.\n\n##### Arguments\n\n1. `url` (String): Request URL. In fact, it could be a part (or prefix) of URL.\n2. `config` (Object): Support `query`, `method`, `url`, `headers`, `cancellation`, `timeout`, and any other options from [fetch api options](https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch)\n\n##### Return\n\n(Object): `ask` instance.\n\n##### Example\n```js\n// es6\nimport Ask from 'http-ask';\n\n// es5\n// var Ask = require('http-ask').default;\n\nconst ask = new Ask('url', {\n\tmethod: 'post',\n\tbody: { ur: 'awesome' }\n});\n```\n\n---\n\n#### Static Method: Ask.create([url[, config]])\n\nThe same with `new Ask()`.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Static Method: Ask.request(url[, config])\n\nShort hand for `Ask.create(url, config).exec()`;\n\n##### Return\n\n(Promise): A promise to get response data.\n\n---\n\n#### Static Method: Ask.clone(ask)\n\nThe same with `ask.clone()`.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Static Property: Ask.Cancellation()\n\nSee the follow `Cancellation` section for detail.\n\n##### Return\n\n(Object): `cancellation` instance, which has a `cancel` method.\n\n---\n\n#### Method: ask#method(method)\n\nSet HTTP request method\n\n##### Arguments\n\n1. `method` (String): All HTTP methods are supported. Default to `get`.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#get([url])\n\nSet `GET` method and url.\n\n##### Arguments\n\n1. [`url`] (String): Request URL.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#post([url])\n\nSet `POST` method and url.\n\n##### Arguments\n\n1. [`url`] (String): Request URL.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#put([url])\n\nSet `PUT` method and url.\n\n##### Arguments\n\n1. [`url`] (String): Request URL.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#patch([url])\n\nSet `PATCH` method and url.\n\n##### Arguments\n\n1. [`url`] (String): Request URL.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#delete([url])\n\nSet `DELETE` method and url.\n\n##### Arguments\n\n1. [`url`] (String): Request URL.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#url(url)\n\nSet or join URL.\n\n##### Arguments\n\n1. `url` (String): Request URL.\n\n##### Return\n\n(Object): `ask` instance.\n\n##### Example\n```js\n// `url` doesn't start with `/`\nAsk\n\t.create('http://you.are')\n\t.url('very/very')\n\t.url('awesome')\n\t.exec()\n\t// the final url is: 'http://you.are/very/very/awesome'\n;\n\n// `url` starts with '/'\nAsk\n\t.create('http://you.are')\n\t.url('very/very')\n\t.url('/awesome') // start with `/`\n\t.exec()\n\t// the final url is: 'http://you.are/awesome'\n;\n```\n\n---\n\n#### Method: ask#query(query)\n\nSet URL query.\n\n##### Arguments\n\n1. `query` (Object): URL query `JSON`.\n\n##### Return\n\n(Object): `ask` instance.\n\n##### Example\n```js\nAsk\n\t.create('http://localhost', {\n\t\tquery: { a: 1, b: 2 },\n\t})\n\t.query({ b: 3, c: 4 })\n\t.query({ c: 5 })\n\t.exec()\n\t// the final url is: 'http://localhost/?a=1\u0026b=3\u0026c=5'\n;\n```\n\n---\n\n#### Method: ask#body(body)\n\nSet HTTP request body.\n\n##### Arguments\n\n1. `body` (Object): A `JSON` or instance of `FormData` as usual.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#set(headerKey, headerValue)\n\nSet HTTP request header.\n\n##### Arguments\n\n1. `headerKey` (String): Header key.\n2. `headerValue` (String): Header value.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#parser(parser)\n\nAdd a response parser.\n\nA parser is a function that receives two arguments:\n\n1. `data` (Any): The response data\n2. `response` (Response): The Response instance\n\nParser should return a promise. The promise value will be passed to the next parser.\n\n##### Arguments\n\n1. `parser` (Function): Response parser.\n\n##### Return\n\n(Object): `ask` instance.\n\n##### Example\n```js\nAsk\n\t.create('http://localhost/test')\n\t.parser((data, response) =\u003e {\n\t\tconsole.log('Status:', response.status);\n\t\tPromise.resolve('awesome!!!');\n\t})\n\t.parser((data, response) =\u003e {\n\t\tconsole.log('Data:', data);\n\t\treturn data;\n\t})\n\t.exec()\n;\n// will log:\n\n// Status: 200\n// Data: awesome!!!\n```\n\n---\n\n#### Method: ask#timeout(ms)\n\nSet HTTP request timeout.\n\n##### Arguments\n\n1. `ms` (Number): Timeout(ms). Defaults to 30000.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#cancellation(cancellation)\n\nSet a cancellation token. See the follow example for detail.\n\n##### Arguments\n\n1. `cancellation` (Cancellation).\n\n##### Return\n\n(Object): `ask` instance.\n\n##### Example\n\n```js\nimport Ask, { Cancellation } from 'http-ask';\n\nconst cancellation = new Cancellation();\n\nsetTimeout(() =\u003e {\n\tcancellation.cancel(); // trigger cancel\n}, 0);\n\nreturn Ask\n\t.create('http://localhost/')\n\t.cancellation(cancellation) // register a cancellation\n\t.exec()\n\t.then(() =\u003e assert(false, 'should not go here'))\n\t.catch((err) =\u003e assert(err instanceof Cancellation))\n;\n```\n\n---\n\n#### Method: ask#clone()\n\nClone `ask` with current config.\n\n##### Return\n\n(Object): `ask` instance.\n\n---\n\n#### Method: ask#exec()\n\nExecute request.\n\n##### Return\n\n(Promise): A promise to get response data.\n\n---\n\n\n#### Property: ask#response\n\nHttp Response instance. It is `null` before `.exec()`.\n\n##### Example\n\n```js\nconst ask = new Ask('http://localhost/');\nask.exec().then((data) =\u003e {\n\tconsole.log('response data', data);\n\tconsole.log('response status', ask.response.status);\n});\n```\n\n---\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcap32%2Fhttp-ask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcap32%2Fhttp-ask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcap32%2Fhttp-ask/lists"}