Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of response data #265

Open
jenstroeger opened this issue May 17, 2017 · 0 comments
Open

Incorrect handling of response data #265

jenstroeger opened this issue May 17, 2017 · 0 comments

Comments

@jenstroeger
Copy link

jenstroeger commented May 17, 2017

It seems to me that the Content-type of a response is handled incorrectly. For example, if I create a request sending JSON data:

  var options = {                                                                                   
    ...
    contentType: 'application/json',                                                                
    type: 'json',                                                                                   
    ...
  };
  var req = reqwest(options);

but the server responds with anything else (e.g. application/pdf) then Reqwest still attempts to decode the response body as JSON. Looking at the code, the culprit seems to be the success() function:

  function success (resp) {
      var type = o['type'] || resp && setType(resp.getResponseHeader('Content-Type')) // resp can be undefined in IE
      ...

here, o['type'] picks the content type of the request, thus prioritizing it over the content type of the response. Further down, this causes the mentioned attempt to decode as JSON what is not JSON.

As a fix, I’d propose to either swap the two sides of the || operator, or to completely remove the o['type'].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant