Search code examples
phpapicurlcontent-lengthamadeus

amadeus api faultstring Content-Length is missing stackoverflow


I'm trying to get my fist Amadeus API call to work.

•• I'm able to retrieve a token ••

$url = 'https://test.api.amadeus.com/v1/security/oauth2/token';
$curls = curl_init();
curl_setopt($curls, CURLOPT_URL, $url);
curl_setopt($curls, CURLOPT_POST, true);
curl_setopt($curls, CURLOPT_POSTFIELDS, 'grant_type=client_credentials&client_id=--key--&client_secret=--secret--');
curl_setopt($curls, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$token = curl_exec($curls);
curl_close($curls);

but after I get the token, I can't go further....

When I try this code

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT=>469,
CURLOPT_URL => "https://api.amadeus.com/v1/shopping/flight-dates?origin=NYC&destination=LON&oneWay=false&nonStop=false",
CURLOPT_RETURNTRANSFER => true, 
CURLOPT_ENCODING => "", 
CURLOPT_MAXREDIRS => 10, 
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array("Authorization: Bearer --token--")
));

$response = curl_exec($curl); $err = curl_error($curl); curl_close($curl);

echo $response;

I get

{"fault":{"faultstring":"Content-Length is missing","detail":{"errorcode":"messaging.adaptors.http.flow.LengthRequired"}}}

What I'm missing ?


Solution

  • Please find a working example below:

    $url = 'https://test.api.amadeus.com/v1/shopping/flight-dates?origin=NYC&destination=MOW&oneWay=false&nonStop=false';
    $curls = curl_init();
    curl_setopt($curls, CURLOPT_URL, $url);
    
    curl_setopt($curls, CURLOPT_HTTPHEADER, array('Authorization: Bearer access_token'));
    $result = curl_exec($curls);
        if (curl_errno($curls)) {
            echo 'Error:' . curl_error($curls);
        }
    print_r ($result);
    curl_close ($curls);
    

    Note:

    • I changed the destination as NYC to LON is not part of the dataset we have in the test environment (you can find the list here).
    • the URL you used was api.amadeus.com which is the production environment but you get a token from test.api.amadeus.com which is the test environment. In the example, I call the test environment.