ethanent / phin

Node HTTP client
MIT License
576 stars 33 forks source link

Add JSON Post/Put support #16

Closed cryptiklemur closed 6 years ago

cryptiklemur commented 6 years ago

Add support for posting/putting JSON.

Would be cool if we could just do this:

p({url: "some url", method: "POST", json: {foo: 'bar'});

Which would set the Content-Type and Accepts headers to application/json

ethanent commented 6 years ago

Thanks for the suggestion. This feature already kind of exists! The data phinOptions option is automatically JSON stringified if it's an object. (The Content-Type header isn't automatically set, but that's really easy for developers to do if they'd like.)

See the phinOptions type definition for more details.

matteocontrini commented 6 years ago

@ethanent I think the Content-Type should be added automatically, I've just spent the last 20 minutes trying to understand why the server was ignoring my POST data...

thelinuxlich commented 6 years ago

Why close this issue? Any API sending JSON without these headers is totally wrong.

ethanent commented 5 years ago

@matteocontrini @thelinuxlich Thanks for the suggestion.

As of phin@2.0.0, the Content-Type header is defined automatically for JSON.stringify-ed request bodies. :)