Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

getJsonRawBody not working when sending data via GET

I am working on an API using Phalcon. I am in the trial/error phase learning both Phalcon and the proper way to build a REST API using this framework.

My issue is that when making an ajax call via jQuery to my API with type: "GET" Phalcon is not able to parse the request using getJsonRawBody. When I run the same ajax call via jQuery with type: "POST" it works.

Here is my ajax request.

$('.start').click(function() { var arr = {timestamp:'<?=time();?>'}; $.ajax({ type: "GET", beforeSend: function (request) { request.setRequestHeader("X-Api-Key", "<?=$api_key;?>"); request.setRequestHeader("X-Hash", "<?=$hash;?>"); }, data: JSON.stringify(arr), contentType: 'application/json; charset=utf-8', url: "http://api-dev.fancompetition.com/my-rest-api/api/robots/", }) .done(function(data) { $('#data').html(data); }); });

On the server side I have:

$vars = $app->request->getJsonRawBody();

So now if you change type to POST I am able to get the contents. When type is GET than my variable vars is blank. Any idea as to why?



4.1k
Accepted
answer

Got the answer.

Basically GET request typically does not have a message body. Pass as a parameter in the url and capture it properly on the server side.