Skip to content

Commit 9aff99e

Browse files
Orie Steeledanieldaf
Orie Steele
authored andcommitted
feat: add support for custom headers to send-request (ipfs-inactive#741)
* add support for custom headers to send-request * add custom headers test * add custom header documentation * fix: clone config.headers to avoid prev headers in next req License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 63d1e22 commit 9aff99e

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"
161161
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]"
162162
```
163163

164+
### Custom Headers
165+
166+
If you wish to send custom headers with each request made by this library, for example, the Authorization header. You can use the config to do so:
167+
168+
```
169+
const ipfs = IpfsApi({
170+
host: 'localhost',
171+
port: 5001,
172+
protocol: 'http',
173+
headers: {
174+
authorization: 'Bearer ' + TOKEN
175+
}
176+
})
177+
```
178+
164179
## Usage
165180

166181
### API

src/utils/send-request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function requestAPI (config, options, callback) {
106106
delete options.qs.followSymlinks
107107

108108
const method = 'POST'
109-
const headers = {}
109+
const headers = Object.assign({}, config.headers)
110110

111111
if (isNode) {
112112
// Browsers do not allow you to modify the user agent

test/custom-headers.spec.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const isNode = require('detect-node')
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(dirtyChai)
9+
10+
const IPFSApi = require('../src')
11+
const f = require('./utils/factory')
12+
13+
describe('custom headers', function () {
14+
// do not test in browser
15+
if (!isNode) { return }
16+
this.timeout(50 * 1000) // slow CI
17+
let ipfs
18+
let ipfsd
19+
// initialize ipfs with custom headers
20+
before(done => {
21+
f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => {
22+
expect(err).to.not.exist()
23+
ipfsd = _ipfsd
24+
ipfs = IPFSApi({
25+
host: 'localhost',
26+
port: 6001,
27+
protocol: 'http',
28+
headers: {
29+
authorization: 'Bearer ' + 'YOLO'
30+
}
31+
})
32+
done()
33+
})
34+
})
35+
36+
it('are supported', done => {
37+
// spin up a test http server to inspect the requests made by the library
38+
const server = require('http').createServer((req, res) => {
39+
req.on('data', () => {})
40+
req.on('end', () => {
41+
res.writeHead(200)
42+
res.end()
43+
// ensure custom headers are present
44+
expect(req.headers.authorization).to.equal('Bearer ' + 'YOLO')
45+
server.close()
46+
done()
47+
})
48+
})
49+
50+
server.listen(6001, () => {
51+
ipfs.id((err, res) => {
52+
if (err) {
53+
throw new Error('Unexpected error.')
54+
}
55+
// this call is used to test that headers are being sent.
56+
})
57+
})
58+
})
59+
60+
after(done => ipfsd.stop(done))
61+
})

0 commit comments

Comments
 (0)