Advanced REST client does not send POST data - php

I have this simple code
<?php
$json = array("status" => $_POST['name']);
header('Content-type: application/json');
echo json_encode($json);
?>
and when i send POST data with Advanced REST cliend, allways have an empty $_POST table.

You are using wrong transport method. If you want to read POST data in $_POST array you have to send it either as multipart or www form urlencoded.
To read the request body you have to use following code:
$postdata = file_get_contents("php://input");
Then you can parse the JSON and transform it to object.
If you want to read the data from the request using $_POST array you need to set Content-Type header to application/x-www-form-urlencoded and send the data as:
param-name=param+value
(note that it is url encoded).

Related

How do I retrieve a token from a post to PHP?

I want to retrieve the token as seen in the developer console image below. How do I do this?
PHP gets the raw request, and if you send JSON content it can get the raw data with the below method:
$postRaw = file_get_contents("php://input");
print_r($postRaw);
Sometimes $_POST return empty but if $_POST return data , you can basically use $_POST['postname']:
print_r($_POST);

Reading Request Payload in PHP

How can I read content of Request Payload (POST) using PHP?
I´m using a Angular aplication to send the POST.
I've tried with: $_POST, $_FILES and file_get_contents('php://input') ...
None worked.
If you need view x-www-form-urlencoded post data, just use
print_r($_POST);
If you need view json post data, use this
$data=json_decode(file_get_contents('php://input'),1);
print_r($data);
Try:
$payload = file_get_contents('php://input');

Difficulty receiving Json Form-Data Through PostMan

While receiving data on PHP file code is like -
print_r($_POST);
echo $name = $_REQUEST['name'];
I am getting null array
How to get the value while posting ? Here is the headers I used
use x-www-form-urlencoded while posting data.Then
echo json_encode($_POST);//prints into json format
Try it will works.
NOTE: By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, "application/json". When we want to post the value as a FORM post, we need to change the serialization algorithm and post the data with the content-type, "application/x-www-form-urlencoded".

POST data not received on server side

I use Postman (the Chrome app) to send POST data to a URL but the POST data are never received by the PHP file, no matter how I change the content-type before sending. Is there a server setting on Apache that stops post data from external sources?
This is the URL:
http://friendesque.com/arranged/handler.php
And this is the content of the handler.php file:
<?php
echo("Inside file");
echo("JSON:");
$json = file_get_contents('php://input');
echo($json);
echo("POST:");
print_r($_POST);
echo("GET:");
print_r($_GET);
?>
In order for data to be available in $_POST array, the following conditions should be met:
Request must be sent with POST HTTP method
Content-Type header must be set to application/x-www-form-urlencoded
Request payload (body) must be in the form of URL-encoded parameters, e.g.
param1=a&param2=b
I sent to your URL a request that meets those 3 conditions and got my data available in $_POST array.
Use file name like /index.php . It will work !!

Send json as request body or POST json in a variable

I am trying to figure out, is there any way of sending raw json to php instead of POST parameter. If it is possible then which way is best i mean either request body or POST parameter.
Send an HTTP request which has a JSON string as its request body (here: using curl on the command line):
$ curl -d '{"foo":"bar"}' example.com/test.php
Read this request body in PHP:
$json = file_get_contents('php://input');
Decode it:
$data = json_decode($json, true);
PHP's $_POST superglobal is automatically populated as an array if the request body of a POST request contains URL encoded key-value pairs (e.g. foo=bar&baz=42). In the above example you're still using an HTTP POST request with "POST data". It just doesn't automatically end up in $_POST because PHP doesn't know how to decode JSON automagically.

Categories