-
Notifications
You must be signed in to change notification settings - Fork 299
feat: add support for custom headers to send-request #741
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OR13 thanks for adding this in. Mind adding a test and documenting this option?
I have added a test, and the build is passing in node 8, but not node 6. |
@diasdavid is a passing node 6 build a requirement for this PR? likewise, this test only covers the node case. is that acceptable? |
I think we may also need to add documentation, although I'm not completely sure where that lives. My best guess would be some mention in README.md. |
@prettymuchbryce I added a short blurb under the CORS setup. I misinterpreted the documentation request initially, sorry for the delay. If you would like me to add additional detail about why custom headers are useful, I can do that. I'm trying to keep this as simple as possible, as its not an option everyone will need. |
Your short blurb in the README makes sense to me. I think it's perfectly fine for now. In terms of the tests, it looks like all of them are failing so I'm guessing it's not related to your change. It looks like Jenkins may be out of disk space. I'm not sure who's responsible for taking a look into that. |
Jenkins is failing, but travis is passing: https://travis-ci.org/ipfs/js-ipfs-api/builds/371638643
|
@diasdavid Mind pointing us in the right direction with the testing issues? Anything we can do to help? |
I've rerun the tests on jenkins as it was still using a old nodejs version we switched yesterday. Change was made here: ipfs-inactive/jenkins-libs@54eca5d I rerun with 8.11.1 activated, testrun is here: https://ci.ipfs.team/blue/organizations/jenkins/IPFS%2Fjs-ipfs-api/detail/PR-741/6/pipeline |
port: 5001, | ||
protocol: 'http', | ||
headers: { | ||
authorization: 'Bearer ' + TOKEN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with the http server lib we're using here -- does it transparently convert header keys to camel case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lgierth I've often wondered about casing of http headers, especially across languages:
https://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive
Here are come examples of headers being set prior to this change.
https://github.com/ipfs/js-ipfs-api/blob/master/src/utils/send-request.js#L113
Its worth noting that user agent header will be overwritten by this code even if its provided in the config.
The headers object is passed directly to the request library:
https://github.com/ipfs/js-ipfs-api/blob/master/src/utils/send-request.js#L162
https://github.com/ipfs/js-ipfs-api/blob/master/src/utils/request.js
Here is an interesting blog post on header casing and some challenges it can cause:
https://medium.com/walmartlabs/hacking-node-core-http-module-f2a10ea3028e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
long story short, i don't think header casing should be altered by code in this library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually a pretty hard and annoying problem, particularly in Node.js.
The basic rule of thumb is that you need to preserve casing on user inputs. This is very problematic if you need to check and manipulate headers elsewhere in code.
I wrote a library to deal with this in request
, so it's pretty widely used. https://github.com/request/caseless, basically just wraps the headers object and provides an API for you to manipulate it in a caseless way while still maintaining the case of the user inputs.
The test that is failing, passes for me locally, and passes on travis...
Example expected output:
Not sure what I can do to fix this build issue, any advice is welcome. I see that its WIP: #762 |
@OR13 If all your tests have passed I think that's something we need to solve, hopefully once we have, a rebase and retest should get this passing. I'll come back to you asap. |
@alanshaw Any updates here? Would be great to be able to authenticate with our nodes. |
src/utils/send-request.js
Outdated
@@ -106,7 +106,7 @@ function requestAPI (config, options, callback) { | |||
delete options.qs.followSymlinks | |||
|
|||
const method = 'POST' | |||
const headers = {} | |||
const headers = config.headers || {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need an Object.assign({}, config.headers)
to avoid mutating the headers object passed as config, otherwise subsequent requests will receive the same headers as previous.
Preparing a local merged copy of PR ipfs-inactive#741
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
CI is currently having issues, running the tests locally passes. Thank you @OR13 🚀 |
* 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>
This pull request adds a header object to the config, which can be used to provide default headers on all requests made by this library.
This is useful when IPFS is behind http proxies that require custom headers, such as Authorization.