{"id":24383429,"url":"https://github.com/firstandthird/hapi-req","last_synced_at":"2025-03-12T17:20:40.276Z","repository":{"id":54180187,"uuid":"85490796","full_name":"firstandthird/hapi-req","owner":"firstandthird","description":null,"archived":false,"fork":false,"pushed_at":"2021-03-04T21:13:50.000Z","size":83,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-04T09:40:25.748Z","etag":null,"topics":["hapi-plugin","hapi-v17"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/firstandthird.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-19T16:20:29.000Z","updated_at":"2021-03-04T21:13:48.000Z","dependencies_parsed_at":"2022-08-13T08:31:05.464Z","dependency_job_id":null,"html_url":"https://github.com/firstandthird/hapi-req","commit_stats":null,"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-req","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-req/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-req/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/firstandthird%2Fhapi-req/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/firstandthird","download_url":"https://codeload.github.com/firstandthird/hapi-req/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243259012,"owners_count":20262380,"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":["hapi-plugin","hapi-v17"],"created_at":"2025-01-19T10:14:27.954Z","updated_at":"2025-03-12T17:20:40.236Z","avatar_url":"https://github.com/firstandthird.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hapi-req\n\nhapi-req is a [hapi](https://hapi.dev/) plugin that simplifies making\nboth local and remote HTTP requests. Under the hood hapi-req uses [wreck](https://hapi.dev/module/wreck/).\n\n## Installation\n\n```console\nnpm install hapi-req\n```\n\n## Usage\n\nRegister the plugin with hapi:\n```js\nawait server.register({\n  plugin: require('hapi-req'),\n  options: {}\n});\n```\n\nTo do a simple GET:\n\n```js\nconst payload = await server.req.get('https://zombo.com', {\n  headers: {\n    user-agent: 'a-headless-browser'\n  }\n});\nconsole.log(payload);\n/*\n\u003c!DOCTYPE html PUBLIC ...\n*/\n```\n\nBy default hapi-req directly returns the _payload_ for a request (the part that you're usually most interested in anyway), but you can get the underlying result object if you want:\n\n```js\nconst response = await server.req.get('https://zombo.com', {\n  returnResponse: true,\n  headers: {\n    user-agent: 'a-headless-browser'\n  }\n});\nconsole.log(response);\n/*\n{ result: {\n    statusCode: 200,\n    headers: {\n      'content-length': 1714\n      'content-type': 'text/html;'\n      ...\n  },\n  payload: \"\u003c!DOCTYPE html PUBLIC ...\"\n}\n*/\n```\n\nhapi-req also supports HTTP GET, POST, PUT, DELETE and PATCH:\n\n```js\nawait server.req.post('https://zombo.com', {\n  payload: {\n    login: 'myName',\n    password: '1234'\n  }\n});\n```\n\nYou can also do local requests to the host server using hapi's [_inject_](https://hapi.dev/api/?v=20.1.0#-await-serverinjectoptions) feature by passing the desired pathname:\n\n```js\ntestServer.route({\n  path: '/aLocalRoute',\n  method: 'get',\n  handler: (request, h) =\u003e ({ hello: \"world\" })\n});\nconst payload = await server.req.get('/aLocalRoute', {});\nconsole.log(payload);\n/*\n{ hello: \"world\" }\n*/\n```\n\n## Plugin Options\n\n- __query__\n\n  You pass a query to hapi-req in the normal way (adding a '_?field1=val1\u0026field2=val2_' to the URL you are calling).  But you can also specify a default query in object form that will be appended to every request you make. This comes in handy when your local server has an api key or token for authorizing all incoming requests.\n\n  For example:\n\n  ```js\n  await server.register({\n    plugin: require('hapi-req'),\n    options: {\n      query: {\n        token: '12345'\n      }\n    }\n  });\n\n  await server.req.get('/someRoute?foo=bar');\n  ```\n\n  Will result in hapi-req making a GET request to _/someRoute?foo=bar\u0026token=12345_ on the local server.\n\n- __localPrefix__\n\n  Set this to have hapi-req add the indicated prefix to every local URL request. So\n\n  ```js\n  await server.register({\n    plugin: require('hapi-req'),\n    options: {\n      localPrefix: '/api'\n    }\n  });\n\n  await server.req.get('/login');\n  ```\n\n  will result in hapi-req making a request to _/api/login_/.  This only works for local requests, requests to remote servers are not affected.  Default is false.\n\n- __maxRetries__\n\n  When an HTTP request times out, you can have hapi-req go ahead and retry the request until the server responds. _maxRetries_ specifies the number of times hapi-req will retry a request before giving up and throwing an error response.  Default is 0.\n\n- __slowWarningRemote__\n\n  You can tell hapi-req to print out some timing info with _server.log_\n  any time a remote request takes too long to respond. Just set _slowWarningRemote_ to the number of milliseconds you consider 'delayed', any request that takes longer to complete will be logged. Default is false, so you must set _slowWarningRemote_ to a value if you want hapi-req to log slow response times.\n\n- __slowWarningLocal__\n\n  Same as _slowWarningRemote_, except this applies only to requests made to the local server (i.e. you can log delayed responses from your local server while ignoring delayed responses from remote servers).  Default is false, you must set _slowWarningLocal_ to a value if you want to see logging info for slow responses.\n\n- __json__\n\n  This is passed to wreck's _json_ parameter.  Valid values are:\n  - _false_ (always returns raw payload)\n  - _true_ (only returns JSON if the content-type is JSON)\n  - _\"strict\"_ (returns JSON if the content-type is JSON but returns an error if the result is not JSON)\n  - _\"force\"_ (always presumes the response is JSON).\n\n  Default in hapi-req is 'force'.\n\n- __logErrors__\n\n  When true, hapi-req will automatically log request errors (in addition to throwing the error for you to process).  Otherwise hapi-req will just throw the error and you can handle logging it yourself.  Default is false.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirstandthird%2Fhapi-req","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffirstandthird%2Fhapi-req","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffirstandthird%2Fhapi-req/lists"}