# @pubcore/http-client

> http client based on axios

Latest version **2.6.1** (published 2021-01-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @pubcore/http-client
pnpm add @pubcore/http-client
yarn add @pubcore/http-client
bun add @pubcore/http-client
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.6.1 |
| Published | 2021-01-08 |
| First published | 2018-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | pubcore |
| Maintainers | dominix, jacklrs, fidorw |
| Keywords | httpclient |

## Links

- npm: https://www.npmjs.com/package/@pubcore/http-client
- Repository: https://github.com/pubcore/http-client
- Homepage: https://github.com/pubcore/http-client#readme
- Issues: https://github.com/pubcore/http-client/issues
- npm.io page: https://npm.io/package/@pubcore/http-client

## Dependencies (3)

- [qs](https://npm.io/package/qs.md) ^6.9.4
- [axios](https://npm.io/package/axios.md) ^0.21.1
- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2

## Recent versions

- 2.6.1 (latest) — 2021-01-08
- 2.6.0 — 2020-09-27
- 2.5.1 — 2020-03-20
- 2.5.0 — 2019-10-15
- 2.4.2 — 2019-08-29
- 2.4.1 — 2019-08-12
- 2.4.0 — 2019-08-12
- 2.3.3 — 2019-08-03
- 2.3.2 — 2019-06-02
- 2.3.1 — 2019-05-30
- 2.3.0 — 2019-05-13
- 2.2.0 — 2018-11-18
- 2.1.0 — 2018-11-17
- 2.0.1 — 2018-11-12
- 2.0.0 — 2018-11-12
- … 3 more at https://npm.io/package/@pubcore/http-client/versions

## README

## http request promise (based on axios http client)

#### Install

	npm i --save @pubcore/http-client

#### Example

	import request from '@pubcore/http-client'

	//a get request
	request({uri:'https://example.com', query:{id: 42}}).then(
		result => {
			//do something with result ...
		},
		err => {
			//do someging with error ...
		}
	)

	//post some json, with basic auth
	request({uri:'https://example.com', method:'post', username:'foo', password:'...', data:{name:'T-Bone'}, query:{id:42}}).then(
		res => {
			//show feedback
		},
		err => {
			//do someging with error ...
		}
	)


#### Features (test descriptions)

	✓ defaults to method get and accept application/json
	✓ rejects to {code:"REQUEST_ERROR", ...}, e.g. on timeout
	✓ rejects 4xx or 5xx to {code:"HTTP_ERROR", ...} (this is expected, not an exception)
	✓ serializes query params in "qs" package default format
	✓ has "authorization" argument to forward authentication
	✓ exports function to create basic-auth value for "authorization" agrument
	✓ has usernaame, password arguments for basic authentication
	✓ has userAgent argument to define own one
	✓ has "data" argument, treated as json (default) for post and put
	✓ has "contentType" argument, if set to "urlEncoded" data is converted accordingly
	✓ supports optional headers
	✓ "__Host-Csrf-Token" cookie is forwarded via HTTP header "X-Csrf-Token" for POST, PUT and DELETE requests

#### rejected error object

Example if request failed (no response exists)

	{
		code:'REQUEST_ERROR',
		details: {
			message: 'timeout',
			uri: 'https://example.com/foo?id=12',
			method: 'get'
		}
	}

Example if request succeeded, but there is a http error (response exists)

	{
		code:'HTTP_ERROR',
		details:{
			body:{status:{code:'ERROR'}},
			headers:{'content-type': 'application/json'},
			message:'Request failed with status code 500',
			status:500,
			method:'get',
			uri:'https://www.example.com/500'
		}
	}

#### If there is a response, the promise is resolved to following result object
Example for "json" response:

	{
		status: 200,
		headers: {'Content-Type': 'application/json' //,...}
		body: {foo: bar}
	}

#### References
[CQRS protection](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html)
["__Host-" cookie prefix](https://tools.ietf.org/html/draft-west-cookie-prefixes-05)

---
_Source: https://npm.io/package/@pubcore/http-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
