简体   繁体   中英

How to send a “proper” POST request to an API

I am currently trying to send a POST request to an API, but I can't seem to wrap my mind around it.

What I need to do is send a POST request like described here:

POST /v2/sessions HTTP/1.1
Host: api.etilbudsavis.dk
Content-Type: application/json
Accept: application/json
{
  "api_key": "YOUR_APP_KEY"
}

And this is one of the ways i've tried to handle it using curl library:

$curl = curl_init("https://api.etilbudsavis.dk/v2/sessions");

$postData = array(
        'api_key' => '{'.$api_key.'}'
);

curl_setopt_array($curl, array(
    CURLOPT_POST => TRUE,
    CURLOPT_HTTPHEADER => array(
        "Host: localhost",
        "Content-Type: application/json",
        "Accept: application/json",
        "Origin: localhost"),
    CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($curl);
if($response === FALSE){
    echo 'Error<br>';
}


$responseData = json_decode($response, TRUE);

// Print the date from the response
print_r($responseData);

print_r(curl_error($curl));

My output in the browser:

Error 
SSL certificate problem: unable to get local issuer certificate

I hope someone can help me out.

Any help will be greatly appreciated.

You're sending a bogus Host: header.

This header is intended to tell a server hosting multiple virtual hosts on a single IP address which vhost the request is intended for. Curl will set this header for you automatically and should only be manually overridden if you're trying to circumvent DNS for development/testing of the destination machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM