bytespider/jsOAuth — Issue #26: postJSON - Content-Type: 'application/json'
Author: ericktai
Created: 2011-11-15 20:25:22+00:00
State: open

Hi Rob,

I thought about other things that people ran into and thought of this recent support ticket. Again, great job on this library, it's helped people get up and running with JS and OAuth really quickly. Well written GitHub docs by the way. I was able to get my example running in < 10 minutes while watching TV :)

The users are basically sending/retrieving JSON through a REST API. Somebody was trying to use OAuth.postJSON to send up some JSON but it wasn't working for him. I think it's because StackMob expects the header 'Content-Type': 'application/json'. postJSON seems to call OAuth.post which in turn doesn't set that header.

I hence had him do this, since it looked like the postJSON was a convenience wrapper to stringify the json object.

oauth.request( {
method : 'POST',
headers: { 'Content-Type': 'application/json' },
url : ..., //your url
data: JSON.stringify(yourobject), //it looks like postJSON is simply a convenience wrapper that converts your object to a string via JSON.stringify
success: function(data) { console.debug(data); }, //again, using chrome console to view output
failure: function(data) { console.error('fail'); console.debug(data); }
});

Would you be able to punch a hole into post/postJSON to pass in headers - and/or should the headers for postJSON have "application/json" by default?

Erick