0

I try to mock a foreign API for testing purposes. However, when I build the endpoint, I cannot access the data sent via post request.

This is the request, that works totally fine with the original API (created with postman)

curl_setopt_array($curl, array(
      CURLOPT_URL => 'https://myEndpointUrl.com/endPoint',
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS =>'{
            "custAddrZip":"12345",
            "custAddrCountry":"de",
            "custGender":"m",
            "products":["123456"],
            "shopDocumentId": "4567890"
            }',
      CURLOPT_HTTPHEADER => array(
            'Content-Type: application/json',
            'X-API-Key: AUTHORIZATION_KEY'
        ),
    )
);

$response = curl_exec($curl);

curl_close($curl);
echo $response;

on the endpoint side there is currently just:

var_dump($_POST);
var_dump(file_get_contents("php://input"));

which returns

array(0) {
}
string(0) ""

where I expect to get the Json string instead.

How can I access the data sent in POST?

Levi Pike
  • 133
  • 6
  • 1
    It seems `CURLOPT_POST` is missing? Not sure if `CURLOPT_CUSTOMREQUEST` can replace it? – KIKO Software Apr 13 '23 at 13:31
  • I assume you do a `curl_exec()` somewhere? – RiggsFolly Apr 13 '23 at 13:34
  • Well, the exact same call works with the original API. I don´t know how they do it, so they can access the data. – Levi Pike Apr 13 '23 at 13:35
  • 1
    It might be that the content is there in the post, but PHP might require the `application/x-www-form-urlencoded` content header? OK, this is all just speculation. I haven't tested anything. – KIKO Software Apr 13 '23 at 13:36
  • 1
    This looks like a good read https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl – RiggsFolly Apr 13 '23 at 13:42
  • @KIKO Software "Content-Type: application/json" is required by the original API. – Levi Pike Apr 13 '23 at 13:44
  • @RiggsFolly thank you, but there the problem seems to be on the requesting side. The Request works perfectly with the original APIs endpoint. My problem seems to be at my mockup API´s receiving endpoint. – Levi Pike Apr 13 '23 at 13:55
  • 2
    Check what `$_SERVER['REQUEST_METHOD']` contains. Often such issues are caused by (unintended) redirects, which make the client follow up with a GET request, and thereby losing the POST data. – CBroe Apr 13 '23 at 13:57
  • Given what I know now, I expect `file_get_contents("php://input")` to work, but it doesn't. Did you have a good look in your [browser developer tools](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/What_are_browser_developer_tools)? What does the network tab there report? – KIKO Software Apr 13 '23 at 13:58
  • @CBroe this returns a GET. Thank you, this seems to be the reason for the problem. – Levi Pike Apr 13 '23 at 14:08
  • 1
    @KIKOSoftware how would browser dev tools be relevant here? The request is going from PHP to another PHP script. Browser isn't involved – ADyson Apr 13 '23 at 14:08
  • 1
    @LeviPike in that case it might be worthwhile checking if your mock endpoint has any URL rewrite rules in place, or anything like that. – ADyson Apr 13 '23 at 14:09
  • When I run your code I see `{ "custAddrZip":"12345", "custAddrCountry":"de", "custGender":"m", "products":["123456"], "shopDocumentId": "4567890" }` from the `file_get_contents("php://input")` – RiggsFolly Apr 13 '23 at 14:15
  • But to see it, I did `file_put_contents('debug/dump.txt', file_get_contents("php://input") );` and then looked at the dump.txt file the browser is not involved here – RiggsFolly Apr 13 '23 at 14:18
  • The `$_POST` was empty – RiggsFolly Apr 13 '23 at 14:18
  • Hi all, @CBroe ´s hint was correct. That was what caused the issue. The endpoint redirected the request as a GET, removing the POST. I could already solve it with this hint. We can close this discussion here, I just do not know how :D. – Levi Pike Apr 13 '23 at 14:29

0 Answers0