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.

result.body is empty in Node but result.text is present

See original GitHub issue

I am using superagent in https://github.com/tkesgar/miyako for both Node backend and frontend. However, I found a problem when running this part of server code:

superagent.get(`${ENDPOINT}${id}`)
    .query({ access_token : TOKEN })
    .query(fields ? { fields } : {})
    .query(data)
    .then(result => {
      // callback(result.body)
      callback(JSON.parse(result.text))
    })
    .catch(error => errorCallback(error))

In the promise handling, result.body is {}, but result.text contains the full endpoint response (endpoint is Facebook Graph API). .buffer(true) doesn’t work (result.body is still empty), and content-type is already JSON.

I was able to get it working with JSON.parse(result.text), though. Also, I can use result.body on client-side without problems.

I’ll try to reproduce the problem if I have some time.

Environment:

  • Node 6.3.0 64-bit
  • Windows 10 64-bit

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
focusauruscommented, Nov 9, 2016

+1 on documentation. -1 on presumptive parsing

0reactions
tommedemacommented, Aug 8, 2019

The docs mention using req.header('Accept', 'application/json') but this does not seem to be part of the API anymore. I’m using req.set now, though am often receiving a “Bad Request” error from Facebook (but not always).

Read more comments on GitHub >

github_iconTop Results From Across the Web

result.body is empty in Node but result.text is present #1078
body is still empty), and content-type is already JSON. I was able to get it working with JSON. parse(result. text) , though.
Read more >
res.body is empty in this test that uses supertest and Node.js
I was noticing I only had my result as text instead of json until I set the request header to accept: application/json ....
Read more >
Response - Web APIs - MDN Web Docs - Mozilla
Creates a clone of a Response object. Response.formData(). Returns a promise that resolves with a FormData representation of the response body.
Read more >
5.x API - Express.js
A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body ), or an empty...
Read more >
Fetch - The Modern JavaScript Tutorial
Second, to get the response body, we need to use an additional method call. ... response.text() – read the response and return as...
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