laravel guzzle not working with amadeus api

I am using Guzzle for retrieving data from Amadeus. It works fine with Postman and also if I call with Ajax it works fine but when I want to retrieve data from the controller it says bad request.

laravel guzzle not working with amadeus api

Postman

laravel guzzle not working with amadeus api

    public function agentsTicket(Request $request)
    {
        $client = new Client();

        try {
            $res = $client->Get('https://test.api.amadeus.com/v2/shopping/flight-offers', [

                'headers' => [
                    'Authorization' => ['Bearer','123456789'],
                ],
                'form_params' => [
                    "originLocationCode" => "SYD",
                    "destinationLocationCode" => "BKK",
                    "departureDate" => "2021-11-01",
                    "returnDate" => "2021-11-18",
                    "adults" => "2",
                    "max" => "1",
                ]
            ]);

            $res = json_decode($res->getBody()->getContents(), true);
            dd($res);
        } catch (\Exception $e) {
            $response = $res->getResponse();
            $result =  json_decode($response->getBody()->getContents());
            return response()->json(['data' => $result]);
        }
    }

here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Solution 1

I found the solution the first problem was changing authorization from array to string.
The second problem was ‘form_params’ as #aynber said I changed it to query and now every thing works fine.

    public function agentsTicket(Request $request)
    {
        $client = new Client();

        try {
            $res = $client->Get('https://test.api.amadeus.com/v2/shopping/flight-offers', [

                'headers' => [
                    'Authorization' => 'Bearer 123456789',
                ],
                'query' => [
                    "originLocationCode" => "SYD",
                    "destinationLocationCode" => "BKK",
                    "departureDate" => "2021-11-01",
                    "returnDate" => "2021-11-18",
                    "adults" => "2",
                    "max" => "1",
                ]
            ]);

            $res = json_decode($res->getBody()->getContents(), true);
            dd($res);
        } catch (\Exception $e) {
            $response = $res->getResponse();
            $result =  json_decode($response->getBody()->getContents());
            return response()->json(['data' => $result]);
        }
    }

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply