Suggestion: add JSON.stringify to post if data is a JSON object and contentType is 'application/json'
rw3iss opened this issue · 0 comments
Currently, if a json object is sent as data, it will url-encode the object.
If the server expects JSON, this will kill the request.
Reqwest includes the option to 'processData = false' within the request, but this leaves a JSON object being sent as [object Object].
This should be especially true of the contentType property is set to application/json, otherwise why would we send a url-encoded value if we are telling it to send json?
It would be nice to add an option to stringify a json object instead of url-encoding it, or otherwise auto-detect the application/json contentType and stringify the json object.
I added this in the code (without the option, just an object and contentType check),
in reqwest.js, line 203 inside getRequest():
function getRequest(fn, err) {
var o = this.o
, method = (o['method'] || 'GET').toUpperCase()
, url = typeof o === 'string' ? o : o['url']
// convert non-string objects to query-string form unless o['processData'] is false
, data = (o['processData'] !== false && o['data'] && typeof o['data'] !== 'string')
? (typeof o['data'] == 'object' && o['contentType'] == 'application/json') ? JSON.stringify(o['data']) : reqwest.toQueryString(o['data'])
: (o['data'] || null)
, http
, sendWait = false
...
If anyone doesn't see an objection to this, I'll add it as a pull request.