# kn-http

> HTTP client for the browser, written in TypeScript

Latest version **3.0.2** (published 2025-04-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install kn-http
pnpm add kn-http
yarn add kn-http
bun add kn-http
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

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

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2025-04-02 |
| First published | 2025-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Florent VIALATTE |
| Maintainers | karewan |
| Keywords | http, ajax, xhr, typescript |

## Links

- npm: https://www.npmjs.com/package/kn-http
- Repository: https://github.com/Karewan/KnHttpJs
- Homepage: https://github.com/Karewan/KnHttpJs#readme
- Issues: https://github.com/Karewan/KnHttpJs/issues
- npm.io page: https://npm.io/package/kn-http

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 3.0.2 (latest) — 2025-04-02
- 3.0.1 — 2025-03-28
- 3.0.0 — 2025-03-02
- 2.0.1 — 2025-01-22

## README

# KnHttpJS

Javascript HTTP client for the browser.

### Changelog

See the changelog [here](CHANGELOG.md)

### Usage

* Install via your favorite package manager or get the latest version in [dist](dist) folder

	```shell
	pnpm install kn-http
	```

	```shell
	npm install kn-http
	```

* Usage example

	```javascript
	console.log('saveBtn()');

	saveBtn.btnLoading(true);

	KnHttp.postForm("/account/settings",
	{
		currentPassword: currentPassword,
		newPassword: newPassword,
		newPasswordConfirm: newPasswordConfirm
	},
	{
		csrf: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
	})
	.onSuccess(res => {
		console.log('saveBtn() onSuccess()', res);

		if (res.data.success) {
			currentPassword = newPassword = newPasswordConfirm = '';
			notifySuccess("Password updated successfully");
		} else {
			notifyError(res.data.error ? res.data.error : "Unknown error");
		}
	})
	.onError(err => {
		console.error('saveBtn() onError()', err);

		if (err.code == KnHttp.CANCELED_ERROR) {
			return;
		} else if (err.code == KnHttp.HTTP_ERROR) {
			notifyError("Unknown error: HTTP CODE " + err.httpCode);
		} else {
			if(err.code == KnHttp.NETWORK_ERROR) {
				notifyError("Please check your internet connection");
			} else {
				notifyError("Unknown error");
			}
		}
	})
	.onEnd((wasSuccess) => {
		console.log('saveBtn() onEnd()', wasSuccess);

		saveBtn.btnLoading(false);
	});
	```

* Methods

	```javascript
	// HTTP GET request with optional options
	// responseType json
	let req = KnHttp.get('https://mysuperurl', opt);

	// HTTP GET request with optional options
	// responseType text
	let req = KnHttp.getText('https://mysuperurl', opt);

	// HTTP GET download request with optional options
	// forced download to browser and responseType blob
	let req = KnHttp.download('https://mysuperurl', opt);

	// HTTP DELETE request with optional options
	// responseType json
	let req = KnHttp.del('https://mysuperurl', opt);

	// HTTP POST "raw" request with optional options
	// raw body and responseType json
	let req = KnHttp.postRaw('https://mysuperurl', data, opt);

	// HTTP POST "form encoded" with optional options
	// form encoded with "application/x-www-form-urlencoded" header
	// responseType json
	let req = KnHttp.postForm('https://mysuperurl', data, opt);

	// HTTP POST "form data" with optional options
	// form data and responseType json
	let req = KnHttp.postFormData('https://mysuperurl', data, opt);

	// HTTP POST "json" with optional options
	// json body and responseType json
	let req = KnHttp.postJson('https://mysuperurl', data, opt);

	// HTTP PUT "raw" request with optional options
	// raw body and responseType json
	let req = KnHttp.putRaw('https://mysuperurl', data, opt);

	// HTTP PUT "form encoded" with optional options
	// form encoded with "application/x-www-form-urlencoded" header
	// responseType json
	let req = KnHttp.putForm('https://mysuperurl', data, opt);

	// HTTP PUT "form data" with optional options
	// form data and responseType json
	let req = KnHttp.putFormData('https://mysuperurl', data, opt);

	// HTTP PUT "json" with optional options
	// json body and responseType json
	let req = KnHttp.putJson('https://mysuperurl', data, opt);

	// Custom HTTP req with options
	let req = KnHttp.request('https://mysuperurl', method, opt);

	// On progress pourcent callback for upload only
	// return self
	req.onProgress(progress => {
		// progress (number)
	});

	// On req sucess callback
	// return self
	req.onSuccess(res => {
		// res.data (any)
		// res.headers (object)
		// res.httpCode (number)
	});

	// On req error callback
	// return self
	req.onError(err => {
		// err.code (number) => See the err codes bellow
		// err.httpCode (number)
		// err.data (any)
		// err.headers (object)
	});

	// On req ended callback (whatever the final result)
	// return self
	req.onEnd(wasSuccess => {
		// wasSuccess (bool)
	});

	// Abort / cancel the request
	req.abort();
	```

* Error codes

	```javascript
	// Request has been cancelled (using the abort method)
	KnHttp.CANCELED_ERROR;

	// Network error (Server can't be reached, no internet ?)
	KnHttp.NETWORK_ERROR;

	// Http error (validateStatus callback has failed)
	KnHttp.HTTP_ERROR;

	// Unknown error (can be a bad JSON for exemple)
	KnHttp.UNKNOWN_ERROR;
	```

* Properties

	```javascript
	// Get the XMLHttpRequest of the request object
	req._xhr;

	// Return the default options object (you can update the properties)
	KnHttp.DEFAULTS;

	// Return the lib version
	KnHttp.VERSION;
	```

* Options

	```javascript
	let req = KnHttp.request('https://mysuperurl', 'POST', {
		// Request timeout in ms
		timeout = 270_000,
		// Request type (raw, json, form, formData)
		requestType = 'raw',
		// Response type (text, json, blob)
		responseType = 'json',
		// Headers object
		headers = {
			'x-custom-header': 'test'
		},
		// Forced download result of the request to the browser
		download = false,
		// Upload request with progress
		upload = false,
		// XMLHttpRequest.withCredentials
		withCredentials = false,
		// HTTP basic auth
		basicAuth = {
			username: 'username',
			password: 'password'
		},
		// Bearer token
		bearerAuthToken = '838da7ac74d22a0d5b25048632f6eb4ae4ec3c6c007613ecd83336f348f75aa7',
		// CSRF Header name
		csrfHeader = 'X-CSRF',
		// CSRF Token
		csrf = '838da7ac74d22a0d5b25048632f6eb4ae4ec3c6c007613ecd83336f348f75aa7',
		// Raw body content
		body = 'rawbodycontent';
	});
	```

### License

See the license [here](LICENSE.txt)

```
The MIT License (MIT)

Copyright (c) 2022-2025 Florent VIALATTE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```

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