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

Dont assume JSON accept header when json: true #205

Closed
alirussell opened this issue Mar 10, 2017 · 5 comments
Closed

Dont assume JSON accept header when json: true #205

alirussell opened this issue Mar 10, 2017 · 5 comments

Comments

@alirussell
Copy link

alirussell commented Mar 10, 2017

https://github.com/tomas/needle/blob/master/lib/needle.js#L355

Is causing issues for me on a specific API, and although its partly the way that I'm using needle and partly the bad implementation of the API, i think that you shouldn't assume that sending a JSON body should result in a JSON response.

In my particular case, the api in question is the Promoter.io add contact API (http://docs.promoter.apiary.io/#reference/contacts/add-a-contactupdate-contact-attributes/add-a-contact/update-contact-attributes). Im POSTing data to this in JSON, and it returns JSON data, but for some reason it doesn't like accept: application/json, it only likes accept: */* or accept: application/vnd.promoterio.v1.0 (its weird content type it returns, which is just JSON).

The problem I'm having is where I'm using the needle library, its not really possible to modify the json: true option to false, and this is forcing the accept: application/json header and I'm getting 406 responses.

While this is a pretty obscure issue, i don't think you should force the header to accept: application/json. The line above checks if accept is non standard (*/*), but you don't check to see if the default */* is actually passed in to the call. If the user specifically sets */* as a header, you should probably respect that and not then change it.

Make any sense ? :|

@tomas
Copy link
Owner

tomas commented Mar 10, 2017

Yep, I think it does. In the meantime you could try setting the accept header to something different than */* so Needle respects your decision and doesn't override it.

needle.post('some/url', data, { json: true, accept: 'application/vnd.promoterio.v1.0' })

An alternative method is to skip setting json: true and do the work yourself. Something like:

needle.post('some/url', JSON.stringify(data), { content_type: 'application/json' })

@tomas
Copy link
Owner

tomas commented Mar 10, 2017

Oh, sorry. There's no shortcut for the content_type header. The last example should be:

needle.post('some/url', JSON.stringify(data), { headers: { content_type: 'application/json' } })

@tomas
Copy link
Owner

tomas commented Mar 10, 2017

Ok, just pushed an update and new version to npm. It should take care of the scenario you describe. :)

@tomas tomas closed this as completed Mar 10, 2017
@alirussell
Copy link
Author

Thanks @tomas! i had already used the first suggestion of setting accept to application/vnd.promoterio.v1.0 but good to have it fixed, as it was causing a lot of confusion for a while (although thats the APIs fault really).

@tomas
Copy link
Owner

tomas commented Mar 14, 2017

You're welcome @alirussell. Always glad to be of service, specially when I have some spare time, haha.

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

2 participants