question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

POST request JSON must be added to req after the attachment of cookies--why?

See original GitHub issue

The following test passes:

var request = require('supertest');
var superagent = require('superagent');

describe('Example:', function() {
  var agent;
  beforeEach(function(done) {
    agent = superagent.agent();
    var req = request('http://example.com').get('/getsomecookies');
    req.end(function(err, result) {
      if (!err) {
        agent.saveCookies(result.res);
        done();
      } else {
        done(err);
      }
    });
  });

  it('should successfully post some JSON', function(done) {
    var req = request('http://example.com').post('/postsomejson')
    agent.attachCookies(req);
    req.send({my: 'JSON'});
    var csrfToken = (/XSRF-TOKEN=(.*?);/.exec(req.cookies)[1]);
    req.set('X-XSRF-TOKEN', unescape(csrfToken));
    req.end(function(err, result){
      expect(res.status).to.eql(200);
      done();
    });
  });
});

However, simply swapping the attachment of cookies with the setting of the JSON causes the test to fail–the server responds with 403. That is, the following fails:

  it('should successfully post some JSON', function(done) {
    var req = request('http://example.com').post('/postsomejson').send({my: 'JSON'});
    agent.attachCookies(req);
    ...
  });

When I look on the server (express proxied by nginx, using the express.csrf middleware) to see what is going on, in the unsuccessful case the secret loaded from req.session.csrfSecret, which is used to match the X-XSRF-TOKEN header, is undefined. In the successful case, the secret loaded from req.session.csrfSecret is exactly what it should be, namely the secret created by the GET request to /getsomecookies.

Interestingly, if I make the POST request without trying to send any JSON, I at least don’t get a 403 response (though the test would fail with a 400 because I didn’t provide any JSON for the server to do something with). That is, this works too:

  it('should successfully post some JSON', function(done) {
    var req = request('http://example.com').post('/postsomejson');
    agent.attachCookies(req);
    ...
  });

So, what is it about attachCookies that is interfering with the setting of the JSON sent in the request? Clearly the server is seeing something different in each case. Is this problem documented anywhere?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
stuartpbcommented, Mar 7, 2015

Also, this means that agent.get(url).end(cb) doesn’t get its cookies set while agent.get(url,cb) does, which is completely bonkers. I’m assuming this is a regression?

0reactions
stuartpbcommented, Mar 7, 2015

Where is any of this saveCookies/attachCookies stuff documented? I don’t see any of it in https://github.com/visionmedia/superagent/blob/master/test/node/agency.js

Furthermore, depending on its behavior to be used externally at all is a defect, since these functions are specifically marked “private” in the comments preceding their definition.

Read more comments on GitHub >

github_iconTop Results From Across the Web

POST request JSON must be added to req after the ... - GitHub
The following test passes: var request = require('supertest'); var superagent = require('superagent'); describe('Example:', function() { var ...
Read more >
node.js - How do I create a HTTP Client Request with a cookie?
Here's how I think you make a POST request with data and a cookie using just the node http library. This example is...
Read more >
Python Post JSON using requests library - PYnative
Python Post JSON using requests library. The requests module provides a json parameter that we can use to specify JSON data in the...
Read more >
Posting Data and Using Sessions with Requests - KishStats
Let's say you first have to log in/authenticate, which sets a browser cookie that must be sent with each subsequent request. Using httpbin...
Read more >
JavaScript developer reference for Azure Functions
The name property defined in function.json doesn't need to match the name ... For example, to read the content of an HTTP request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found