We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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: "https://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?



6.6k
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.