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

Error: Invalid Json #130

Closed
andela-potemuyiwa opened this issue May 5, 2015 · 6 comments
Closed

Error: Invalid Json #130

andela-potemuyiwa opened this issue May 5, 2015 · 6 comments

Comments

@andela-potemuyiwa
Copy link

You get an error of Invalid Json when you set your Content-Type to be application/json when doing needle.post.

If you don't set Content-Type at all, the post operation works.

Moving forward, needle shouldn't force Content-Type on an options object when it has been explicitly set by the User.

@tomas
Copy link
Owner

tomas commented Oct 2, 2015

Hi,

Can you share a snippet of code? Sorry for the long delay, but I was quite swamped these past months. :)

@tomas
Copy link
Owner

tomas commented Oct 16, 2015

Ping?

@mrft
Copy link

mrft commented Jan 15, 2016

I think my issue is reletaed to this one:

I had some code that used to work and stopped working recently.

I was trying to save a list of documents in bulk to couchdb. You do this by POSTing a document to /_bulk_docs?include_docs=true

So I used to do something like this:

bulk = { "all-or-nothing": true, "docs": myArrayOfDocuments }
bulkString = JSON.stringify( bulk )
needle.post( fullUrl, bulkString, bulkRequestOptions, cb )

And just today it seems I suddenly needed to do the following to make it work:

bulk = { "all-or-nothing": true, "docs": myArrayOfDocuments }
needle.post( fullUrl, bulk, bulkRequestOptions, cb )

In this case, I did have the header set to "Content-Type': "application/json"
but I don't know if that actually makes a difference.

What surprises me is that it stopped working with a string (this code has worked for more than a year), because I should be able to do my own stringifying and be sure that that is what will be sent. Now it feels as if my string is being tampered with afterwards.

@tomas
Copy link
Owner

tomas commented Jun 7, 2016

Needle did have some changes in the way data is fed to the request, but nothing major that should break anything. If you want to send an object as a JSON object, just pass the original object and set json: true in the options.

@tomas
Copy link
Owner

tomas commented Jun 7, 2016

I'm not sure what the problem is, by the way.

@tomas
Copy link
Owner

tomas commented Jun 15, 2016

If you take a look at the tests in post_data_spec.js, you'll see that post requests with a raw string doesn't tamper with your string. It only sets the content-type header to "application/x-www-form-urlencoded", which might be the problem.

In your example above, what does bulkRequestOptions look like? That way I can try to replicate both cases and see what's different.

@tomas tomas closed this as completed in 7d3e5a5 Nov 25, 2016
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

3 participants