result.body is empty in Node but result.text is present
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1 on documentation. -1 on presumptive parsing
The docs mention using
req.header('Accept', 'application/json')
but this does not seem to be part of the API anymore. I’m usingreq.set
now, though am often receiving a “Bad Request” error from Facebook (but not always).