guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regarding POST request bug.

Quanttelium opened this issue · comments

Guzzle version(s) affected: 7.2
PHP version: 8.1

Description
I am trying to integrate Amadeus Self-Service API within the Laravel Environment. I am successfully able to get content by 'GET' request, but I am not able to get content by the POST request. I have set the exceptions to display the errors thrown by the guzzle in specific.

Here is the api reference, which has the data and the endpoint which I want to post to.
https://developers.amadeus.com/self-service/category/air/api-doc/flight-offers-search/api-reference

How to reproduce
This is the method which I call from my Client.php and pass the data through by calling the POST method.

public function __construct() {
    throw_if(static::$instance, 'There should be only one instance of this class');
    static::$instance = $this;
    $this->client = new Client([
        'base_uri' => 'https://test.api.amadeus.com/',
    ]);
}

public function get($uri, array $options = []) {
    $this->authenticate();
    return $this->client->request('GET', $uri, [
        $options, 
        'headers' => [
            'Authorization' => 'Bearer '.$this->access_token,
        ],
    ]);
}

public function post($uri, array $options = []) {
    $this->authenticate();
    return $this->client->request('POST', $uri, [
        $options, 
        'headers' => [
            'Authorization' => 'Bearer '.$this->access_token,
        ],
    ]);
}

After calling the POST method, I pass the 'X-HTTP-Method-Override' as 'GET', and pas the data as body.

$requests_response = $client->post('v2/shopping/flight-offers', [
    'headers' => [
        'X-HTTP-Method-Override' => 'GET',
    ],
    'body' => [
        [
            "currencyCode" => "USD",
            "originDestinations" => [
                [
                    "id" => "1",
                    "originLocationCode" => "RIO",
                    "destinationLocationCode" => "MAD",
                    "departureDateTimeRange" => [
                        "date" => "2022-11-01",
                        "time" => "10:00:00",
                    ],
                ],
                [
                    "id" => "2",
                    "originLocationCode" => "MAD",
                    "destinationLocationCode" => "RIO",
                    "departureDateTimeRange" => [
                        "date" => "2022-11-05",
                        "time" => "17:00:00",
                    ],
                ],
            ],
            "travelers" => [
                ["id" => "1", "travelerType" => "ADULT"],
                ["id" => "2", "travelerType" => "CHILD"],
            ],
            "sources" => ["GDS"],
            "searchCriteria" => [
                "maxFlightOffers" => 2,
                "flightFilters" => [
                    "cabinRestrictions" => [
                        [
                            "cabin" => "BUSINESS",
                            "coverage" => "MOST_SEGMENTS",
                            "originDestinationIds" => ["1"],
                        ],
                    ],
                    "carrierRestrictions" => [
                        "excludedCarrierCodes" => ["AA", "TP", "AZ"],
                    ],
                ],
            ],
        ],
    ],
]);

Additional context
Here is the error, which I caught in the log.

local.ERROR: Guzzle error {"response":{"GuzzleHttp\Psr7\Stream":"
{
"errors": [
{
"code": 38189,
"title": "Internal error",
"detail": "An internal error occurred, please contact your administrator",
"status": 500
}
]
}
"}}

local.ERROR: Server error: POST https://test.api.amadeus.com/v2/shopping/flight-offers resulted in a 500 Internal Server Error response:
{
"errors": [
{
"code": 38189,
(truncated...)
{"exception":"[object] (GuzzleHttp\Exception\ServerException(code: 500): Server error: POST https://test.api.amadeus.com/v2/shopping/flight-offers resulted in a 500 Internal Server Error response:
{
"errors": [
{
"code": 38189,
(truncated...)
at C:\xampp\htdocs\Application\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113)

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.