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.

Ability to force JSON.parse of the response when content-type is not application/json

See original GitHub issue

Superagent should JSON.parse the response when a user sets that he expects a json via accept('json') in the request, no matter what content-type was set in the response header.

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
kornelskicommented, Apr 26, 2016

Try this:

require('superagent').parse['put the wrong type here'] = JSON.parse;
2reactions
eburicommented, Apr 26, 2016

Did anything happen regarding this issue? The workaround proposed by @teebot is not working, as in my case response.body contains an empty object {}.

        request('GET', 'https://<misconfigured-server>/test.json')
            .query({ format: 'json' })
            .set('Accept', 'application/json')
            .end((err, response) => {
                console.log('Result:', response.body);
                done();
            });

The output is: Result: {}

The problem is, that the server does not send a Content-Type header. But I think, that there should be an option to tell superagent to parse the response, no matter what the server sends.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ability to force JSON.parse of the response when content-type ...
Superagent should JSON.parse the response when a user sets that he expects a json via accept('json') in the request, no matter what ...
Read more >
JSON.parse() does not work when content-type is set to ...
When you specify nothing for the dataType , and "json" for the Content-Type header, response is parsed as JSON (Javascript object literal).
Read more >
Handle content types - Azure Logic Apps - Microsoft Learn
By default, you can parse JSON content without any casting. To parse a request that has a header with the "application/json" content type, ......
Read more >
JSON requests and responses - Atlassian Developer
To make a request with JSON, the appropriate HTTP headers are: Copy. 1 2 Content-Type: application/json Accept: application/json ...
Read more >
An Exploration of JSON Interoperability Vulnerabilities
In this research, I conducted a survey of 49 JSON parsers, cataloged their quirks, and present a variety of attack scenarios and Docker...
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