Giter VIP home page Giter VIP logo

Comments (6)

alexfernandez avatar alexfernandez commented on July 29, 2024

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.

bennettrogers avatar bennettrogers commented on July 29, 2024

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.

alexfernandez avatar alexfernandez commented on July 29, 2024

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.

bennettrogers avatar bennettrogers commented on July 29, 2024

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.

alexfernandez avatar alexfernandez commented on July 29, 2024

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.

alexfernandez avatar alexfernandez commented on July 29, 2024

Closing, please reopen if still an issue.

from loadtest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.