Comments (6)
You are reading this right. I am using an undocumented feature of http.writeHead(). This is because headers may have multiple values with the same key, while a map does not allow repeated keys. An array of arrays, while ugly, is the only way to make sure we are not overwriting any previous headers.
We might have a map by default, and then switch to an array for repeated headers, but both code paths should have to be tested separately. Arrays work beautifully all the time.
from loadtest.
In the example provided though, the https module is failing because of the header array. Invoking the https module directly and using an array for the headers causes the same issue. If I instead pass an object, the https module works correctly.
The following test fails with a 400 error (the request is never actually made):
var https = require('https');
var headers = ['Authorization', 'Token mytoken'];
var options = {
hostname: 'twitter.com',
headers: headers,
port: 443,
path: '/',
method: 'GET'
};
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error(e);
});
from loadtest.
Yes, if you look carefully at the link above, you have to pass in a double array, i.e. an array of arrays. In your case it should be:
var headers = [['Authorization', 'Token mytoken']];
The way to pass multiple headers with the same key would be:
var headers = [
['Authorization', 'Token mytoken'],
['Authorization', 'Token secondtoken']
];
That is something that you cannot do with a simple map.
from loadtest.
You're right, I should have nested the array in the second example. Even with that change it still fails with the same error.
It looks like node might have been updated to concatenate duplicate headers since that article was written?
nodejs/node-v0.x-archive#6821
And possibly it rejects header arrays now?
from loadtest.
I think Twitter is just rejecting your authorization token. If I run a test server and send a request against it using your code (host localhost, port 7357) I get the correct headers:
user@host: ~/loadtest $ node bin/testserver.js
[Tue Feb 25 2014 01:24:44 GMT+0100 (CET)] INFO Listening on port 7357
[Tue Feb 25 2014 01:25:43 GMT+0100 (CET)] INFO Headers for GET: { authorization: 'Token mytoken', connection: 'keep-alive' }
There must be a problem in the header format, but it is being sent just fine. At least with node v0.10.25.
from loadtest.
Closing, please reopen if still an issue.
from loadtest.
Related Issues (20)
- node:events:505 throw er; // Unhandled 'error' event HOT 12
- LoadRunner HOT 1
- Displaying political slogans after installing loadtest HOT 7
- How to send multiple headers
- INFO Errors: 10, accumulated errors: 10, 0.2% of total requests HOT 2
- Callback function is not called by anyone, possible dead code HOT 2
- Cannot find package microprofiler imported from /node_modules/loadtest/lib/parser.js HOT 1
- use it globally installed with a generator.js in current dir? HOT 1
- Concurrent Clients mismatch HOT 6
- POST-file has not imported javascript file correctly
- Command-line hanging / not exiting on completion HOT 3
- Can't import in ESM project HOT 1
- Add object in options will got an error HOT 1
- Configuration result is immutable HOT 2
- TypeError: Cannot read property 'id' of undefined HOT 2
- Not handling null 'result' value in statusCallback HOT 1
- SyntaxError: Unexpected reserved word HOT 1
- Readme is incorrect in terms of --cores HOT 1
- Number of total requests is incorrect HOT 1
- docs outdated? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from loadtest.