I've been trying to get my POST body right, but just can't get it.
Requirement:
The post body needs to be something like this
{ user = { "current_password"="pass1234", "password"="pass7890" } }I have constructed the above in an object called params and pass it to my request as follows
xhr.send(params)
The above seems to encode the object, as specified in the Titanium docs for the HTTPClient. Definitely something that will give me an invalid response.
Then I tried xhr.send(JSON.stringify(params)), but the remote server gives a similar response as for the previous call.
I also tried xhr.send('' + JSON.parse(params) + ''). In this case, Titanium sets the body to "[object object]" which is kind of weird, from my perspective. This, obviously, did not work as well.
Can someone help me here, please. How could I tell the HTTPClient, not to encode my POST data/body?
TiSDK: 2.1.1.GA
iPhone running 5.1.1
2 Answers
Hi Nikhil
You need to end with something like this - using you own example as a reference.
var param = { current_password: "pass1234", password: "pass7890" };You can then pass this using;
xhr.send(param);You do not need to stringify the parameters as xhr does it for you, which makes things easy.
This will pass two fields to your API; current_password & password.
So I figured this out for myself. Turns out, I needed to Stringify the params object.
But the real problem was here: I had set a header Content-Type: 'application/www-..... whatever that long string is.
When I set it to Content-Type: 'application/json', everything worked.
Hope this helps someone.
Your Answer
Think you can help? Login to answer this question!