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.

No res.body received from GET

See original GitHub issue

I am testing a Node.js API with supertest, and the res.body object superset returns is empty. The data shows up in the res.text object, but not res.body. This appears to be a bug, as the exact same code works in a similar project.

I am using Express and body-parser:

app.use(bodyParser.json());
app.use(bodyParser.json({ type: jsonMimeType }));
app.use(bodyParser.urlencoded({ extended: true }));

Here is the API method I am testing:

app.get(apiPath + '/menu', function(req, res) {
  var expiration = getExpiration();

  res.set({
    'Content-Type': jsonMimeType,
    'Content-Length': jsonTestData.length,
    'Last-Modified': new Date(),
    'Expires': expiration,
    'ETag': null
  });

  res.json({ items: jsonTestData });
}

Here are the tests I am executing against this API method:

describe('GET /menu', function() {
  describe('HTTP headers', function() {
    it('responds with the right MIME type', function(done) {
      request(app)
        .get(apiPath + '/menu')
        .set('Accept', 'application/vnd.burgers.api+json')
        .expect('Content-Type', 'application/vnd.burgers.api+json; charset=utf-8')
        .expect(200, done);
    });

    it('responds with the right expiration date', function(done) {
      var tomorrow = new Date();
      tomorrow.setDate(tomorrow.getDate() + 1);
      tomorrow.setHours(0,0,0,0);

      request(app)
        .get(apiPath + '/menu')
        .set('Accept', 'application/vnd.burgers.api+json; charset=utf-8')
        .expect('Expires', tomorrow.toUTCString())
        .expect(200, done);
    });

    it('responds with menu items', function(done) {
      request(app)
        .get(apiPath + '/menu')
        .set('Accept', 'application/vnd.burgers.api+json; charset=utf-8')
        .expect(200)
        .expect(function (res) {
          console.log(res);
          res.body.items.length.should.be.above(0);
        })
        .end(done);
    });
  });
});

The failure I receive:

1) GET /menu HTTP headers responds with menu items:
     TypeError: Cannot read property 'length' of undefined
      at /Users/brian/Development/demos/burgers/menu/test/MenuApiTest.js:42:25
      at Test.assert (/Users/brian/Development/demos/burgers/menu/node_modules/supertest/lib/test.js:213:13)
      at Server.assert (/Users/brian/Development/demos/burgers/menu/node_modules/supertest/lib/test.js:132:12)
      at Server.g (events.js:180:16)
      at Server.emit (events.js:92:17)
      at net.js:1276:10
      at process._tickDomainCallback (node.js:463:13)

And finally, here is an excerpt of the result of console.log(res):

...
text: '{"items":[{"id":"1","name":"cheeseburger","price":3},{"id":"2","name":"hamburger","price":2.5},{"id":"3","name":"veggie burger","price":3},{"id":"4","name":"large fries","price":2},{"id":"5","name":"medium fries","price":1.5},{"id":"6","name":"small fries","price":1},{"id":"7","name":"large drink","price":2.5},{"id":"8","name":"medium drink","price":2},{"id":"9","name":"small drink","price":1}]}',
  body: {},
...

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
malthejorgensencommented, Feb 23, 2015

@mikker The issue seems to have been resolved in a question on StackOverflow.

I have the same problem, and my Express application actually does return application/json; charset=utf-8 and yet the result doesn’t have a body property – only the text property. Strange.

0reactions
feytoncommented, Jan 17, 2022

Did you ever solve this?

I faced the same issue using Mocha on node js with Express. I used the expect function to run the test like this import chai, { expect } from "chai"; //omited code expect(res.body.data.length).to.eql(0) //call done()

Read more comments on GitHub >

github_iconTop Results From Across the Web

No res.body received from GET · Issue #171 · ladjs/supertest
I am testing a Node.js API with supertest, and the res.body object superset returns is empty. The data shows up in the res.text...
Read more >
Why res. body is undefined in express - Stack Overflow
log(req) but in express it is from server side to sen data using res object, so its natural to get res.body undefined.expressjs.com/en/api.html# ...
Read more >
Response.body - Web APIs - MDN Web Docs
In our simple stream pump example we fetch an image, expose the response's stream using response.body , create a reader using ReadableStream ...
Read more >
Express body-parser middleware
Parse incoming request bodies in a middleware before your handlers, available under the req.body property. Note As req.body 's shape is based on...
Read more >
Anatomy of an HTTP Transaction - Node.js
Getting at the body data is a little more involved than accessing request headers. ... If we run this example, we'll be able...
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