0

I am trying to send json values ​​with a url by POST but I am not getting any value.

local test: http://127.0.0.1/test.php

with postman I send inside the body:

key: Token Value: xxyyz

my service code:

error_reporting(E_ALL);
ini_set('display_errors', 1);
error_reporting(E_ALL);
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');

$data = file_get_contents('php://input');
$arrayData = json_decode($data);

var_dump($arrayData);

Return NULL, How can I return the values?

Leoh
  • 642
  • 2
  • 17
  • 41

1 Answers1

0

If you send by Postman send body and then click on the option raw. Then you will see in the end of the line more options. (text, Javascript, JSON). Select JSON and insert your JSON String and send it to your server.

... and i hope that will work. I tested with your php code and it works.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • It works for me in raw format, but why doesn't it work for me in format-data? – Leoh Jan 22 '21 at 17:38
  • @Leoh Because php://input is not available with enctype="multipart/form-data". You should not attach the JSON as a file to your request, but add it as a request body to the post request, setting the Content-Type header (application/json). Then it will be available in php://input. Then you have a problem. Once you use enctype="multipart/form-data", php://input will remain empty. – Maik Lowrey Jan 22 '21 at 19:05
  • @Leoh But you can use $_POST instead of file_get_content. With the key you use for form-data, you can fetch your JSON on the serverside. – Maik Lowrey Jan 22 '21 at 19:19