Welcome Guest, Not a member yet? Register   Sign In
How do you get request body data in controller?
#1

[eluser]SpudNik[/eluser]
Hey all..

I am POSTing data to my ci controller using content-type application/json. I know that input->post() will not work. I read somewhere that there is a class that will allow me to get the request body data. Is this documented? I could find anything but input->post Any examples? Basically I just want to get key/value pairs from json being posted to my controller and I'd rather not change the content-type header if possible.

Thanks!
#2

[eluser]jmadsen[/eluser]
"I know that input->post() will not work"

why do you "know" that? because of the content-type?

what have you tried?
#3

[eluser]mast3rpyr0[/eluser]
If you have a json string like so : {'variable': 'data'}
then $this->input->post('variable'); is exactly what you want!
#4

[eluser]SpudNik[/eluser]
This is does not work:

var data = {username:"someuser",password:"somepassword"}

$.ajax({
url:url,
type:"POST",
data:data,
contentType:"application/json",
dataType:"json",
success: function(data){
alert(data);
}
});
}

This does:

$.post(url,data);

The difference between these two AJAX calls is that the first one sets the Content-Type header to "application/json"

$.post on the other hand sets the Content-Type to: application/x-www-form-urlencoded

See this answer: http://stackoverflow.com/questions/85691...st-request

Im new to CI so I'm wondering where I can read about $this->request->body
#5

[eluser]SpudNik[/eluser]
I was able to get this working by using
Code:
$jsonArray = json_decode(file_get_contents('php://input'),true);
The above will read the request body from the input request and then decodes the JSON to an associative array.

I would still be interested in refactoring this code if CI has a wrapper for reading input stream data as I am above. Fairly new to this framework.
Thanks for the replies.
#6

[eluser]Aken[/eluser]
Is there a reason you're setting the content type to JSON? You shouldn't need to specify the content type unless wherever you are sending the data explicitly needs a JSON request. Just because your data is in JSON doesn't mean it's going to stay that way once it goes through the ajax call (it doesn't, btw). You should be able to remove that line, then use $this->input->post() like normal.

Also, the "dataType" parameter is specifying what format the RESPONSE will be in, so if you set that, make sure your response is in JSON and not something else, or your returned data might not work properly.
#7

[eluser]SpudNik[/eluser]
[quote author="Aken" date="1329682589"]Is there a reason you're setting the content type to JSON? You shouldn't need to specify the content type unless wherever you are sending the data explicitly needs a JSON request. Just because your data is in JSON doesn't mean it's going to stay that way once it goes through the ajax call (it doesn't, btw). You should be able to remove that line, then use $this->input->post() like normal.

Also, the "dataType" parameter is specifying what format the RESPONSE will be in, so if you set that, make sure your response is in JSON and not something else, or your returned data might not work properly.[/quote]

Right. The client accepts "application/json". Thus the dataType:json. In terms of JQuery the data can be sent in the same format in which is set. This is done by using the processData:false switch. I just thought that CI had a wrapper object that basically did what I have above. This might be a feature of CI Rest Server. Thanks for the response. What I have works well enough.




Theme © iAndrew 2016 - Forum software by © MyBB