PHP Unirest POST request to travel API returns 405 Method Not Allowed error

I’m working with a flight booking API through RapidAPI and having trouble with my POST request. When I try to create a new pricing session, I keep getting a 405 Method Not Allowed error instead of the expected response with the location header.

Here’s my current code:

require_once 'vendor/autoload.php';
require_once 'vendor/mashape/unirest-php/src/Unirest.php';

$result = Unirest\Request::post("https://travel-flight-api-v1.p.rapidapi.com/apiservices/search/v1.0",
  array(
    "X-RapidAPI-Host" => "travel-flight-api-v1.p.rapidapi.com",
    "X-RapidAPI-Key" => "abc123def456ghi789jkl012mno345pqr678stu901vwx234yz",
    "Content-Type" => "application/x-www-form-urlencoded"
  ),
  array(
    "departureDate" => "2024-05-15",
    "travelClass" => "economy",
    "childCount" => 0,
    "infantCount" => 0,
    "countryCode" => "US",
    "currencyCode" => "USD",
    "localeCode" => "en-US",
    "fromLocation" => "LAX-sky",
    "toLocation" => "JFK-sky",
    "returnDate" => "2024-05-20",
    "passengerCount" => 2
  )
);

var_dump($result);

I should be getting headers like this:

cache-control: private
content-type: application/json
date: Mon, 15 Apr 2024 10:30:15 GMT
location: http://partners.api.travel.net/apiservices/search/us1/v1.0/12ab34cd-567e-89fg-hij1-234567890klm
server: RapidAPI-1.2.8

But instead, I’m getting this error response with a 405 status code and an empty body. The headers indicate Method Not Allowed, but I’m sure I’m using POST correctly. What could be causing this issue?

Had this exact issue with RapidAPI before. You’re probably sending form data when the API wants JSON. Switch your content-type to “application/json” and wrap your data array in json_encode(). Also check the API docs again - travel APIs can be picky about parameter names and might need extra auth on top of the RapidAPI key. That 405 error means the endpoint’s there but doesn’t like how you’re formatting the POST request.

maybe the endpoint only supports get requests? they can be picky, especially with travel APIs. try removing that content-type header too, might help since rapidapi doesn’t like some formats.

First, double-check your endpoint URL - travel APIs often use different paths for search vs booking operations. The 405 error usually means the API wants a different HTTP method or there’s a trailing slash problem. Try adding or removing the trailing slash from your URL. Also check if that endpoint needs a session established first with a GET request before you can POST. I’ve run into travel APIs that make you grab a session token first, then use it in your POST requests. Worth reviewing the API docs for any auth flow requirements beyond just your RapidAPI key.