Wednesday, February 2, 2022

[SOLVED] RAW data POST API using curl not working but executing in POSTMAN is okay

Issue

I am a beginner in PHP and I am trying to put POST data using cURL. I tried in Postman my data and it returns

enter image description here

but when I execute this code below which I found here in StackOverflow response returns me a 307 Temporary Redirect.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,            "https://mycustomURL.com/api/v1/login" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST,           1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,     '{
    "loginid":"test",
    "password":"111111"

}' ); 
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/raw')); 

$result=curl_exec ($ch);


var_dump($result);

Can you help me what is the problem? Thank you.


Solution

To make cURL follow a redirect just add this line:

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

for more info see php.net curl



Answered By - Eden Moshe
Answer Checked By - David Goodson (WPSolving Volunteer)