1

I'm using Insomnia Rest to simulate my frontend JSON data being sent to backend. I tested with Postman as well and the issue is the same:

I send JSON data via POST to backend and, when I print what has come in the $_POST variable, it's empty.

enter image description here

I tried print_r(), json_decode/json_encode aproaches, echo, die(), etc. None of them works. There are two ways I can work with those HTTP requests:

1) Send via "FORM URL Encoded" - this one works pretty well. The problem is it only accepts simple data. It's not possible to create objects for example, which in many cases, is very important to work with;

enter image description here

2) Send via JSON, as mentioned, but in PHP I need to use:

$json = file_get_contents("php://input");

Well, in this case, I already have a more-than-1000-lines code using json_encode/json_decode. Based on this case, JavaScript and PHP seems to do all the conversions needed to send and receive JSON data and thus, HTTP simulation softwares don't.

Has anybody faced something and figured out some workaround?

Raphael Alvarenga
  • 868
  • 1
  • 7
  • 13
  • 2
    Do you have any sort of redirect, such as a HTTP->HTTPS redirect? That can tend to lose data as it changes the method. – aynber Sep 10 '19 at 16:36
  • You always need to get the JSON post body through `file_get_contents("php://input")`, Slim do the [same thing](https://github.com/slimphp/Slim-Psr7/blob/f27d67d13978c2d2f380b9762d822b693ff44c63/src/Factory/ServerRequestFactory.php#L98). If you need something to use `$_POST` with the JSON body post data use [this](https://mdref.m6w6.name/json_post), remember use always the header `application/json` – kip Sep 10 '19 at 16:43
  • Not particularly helpful, but in general, try var_dump() next time you can't get output from a variable. Noticed it wasn't mentioned, and it's too helpful in troubleshooting not to be a first option. – TCooper Sep 10 '19 at 16:45
  • Hello aynber, I'm using my localhost. It's just HTTP... – Raphael Alvarenga Sep 10 '19 at 17:58
  • Hello kip! Thanks for helping. I'll consider the package you shared. – Raphael Alvarenga Sep 10 '19 at 18:02
  • Hello TCooper, thanks for answering. Unfortunately, var_dump() returns array(0) { } – Raphael Alvarenga Sep 10 '19 at 18:03
  • I also expirienced with same problem. I try to send POST request with Basic Auth and JSON body. It always sent it as a GET request. If you select PUT, it seems ok, but body stay empty. – George Novik Jan 20 '21 at 10:20

0 Answers0