Search code examples
phplaravelguzzleamadeus

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.

enter image description here

Postman

enter image description here

    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]);
        }
    }

Solution

  • 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]);
            }
        }