0

my Problem is the following:

I have an Apache Server running on an RaspberryPi. I try to make a http POST with paramaters as follows:

http://192.168.178.43/update-led.php/?api_key=tPmAT5Ab3j7F9

Here you can see a picture of it in Postman: POSTMAN Form

However in the php code which gets called in update-led.php file I get the error:

PHP Notice: Undefined index: api_key in /var/www/html/update-led.php on line 31

Here is the Code from the update-led.php file:

    $api_key = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (isset($_POST["api_key"])){// returns false
        $api_key = test_input($_POST["api_key"]);
           if($api_key == $api_key_value) {
               //do something
           }
        }
      } else{
          echo "No Api Key provided"
      }
  }

I also get the http result "No Api Key provided" which means the isset($_POST['api_key']) command returns false.

I can't find the mistake which I am making and the strange thing is, that the api_key is already working in an http GET request with the following Code:

if ($_SERVER["REQUEST_METHOD"] == "GET") {
   $api_key = test_input($_GET["api_key"]);
   if($api_key == $api_key_value) {
      //do something
   }
}

Thank you for your help, i hope I didn't forget any important Information. Paul

  • What do you get if you do `var_dump($_POST);`? I'm wondering if the fact that you have api_key in both GET and POST is confusing it, but that's just a thought. – aynber Aug 30 '22 at 19:37
  • 1
    You are sending the parameters as GET, to send POST you have to add it under body tab in Postman – Moshe Gross Aug 30 '22 at 20:28
  • I got it working by changing everything to GET instead of POST. Is there a huge difference between those? what are the benefits of POST? – Paul Heming Aug 30 '22 at 20:39
  • https://stackoverflow.com/q/504947/1427878 – CBroe Aug 31 '22 at 06:40

1 Answers1

-2

Please remove the / after update-led.php so the URL is:

http://192.168.178.43/update-led.php?api_key=tPmAT5Ab3j7F9
Tim Lewis
  • 27,813
  • 13
  • 73
  • 102