S. N
S. N

Reputation: 3949

ReactJS making a cross domain request using POST and GET fails with code 405

I am trying to make a cros request to skyscanner and get some information. I have studied their doc and here is what I need to do:

  1. make a session using POST request,
  2. then get a response header,
  3. then use that url to make GET request,
  4. to get flight data.

So basically there are 2 API calls.

This is the code I have:

export function getFlights() {

    const request = axios.post(
        'http://partners.api.skyscanner.net/apiservices/pricing/v1.0/',  
        JSON.stringify({
            "cabinclass": "Economy",
            "country": "UK",
            "currency": "GBP",
            .
            .
            .
            .
            "apikey": "apikey"
        }), 
        {   
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
        .then(function (response) {
            console.log(response.headers);
            axios.get(response.headers.location + '?apiKey=apikey&stops=0&duration=360&includeCarriers=ba;u2;af').then(function(response) {
                console.log(response)
            })
        })
        .catch(function (error) {
            console.log(error);
        });
    return {
        type: GET_FLIGHT, 
        payload: request
    };
}

So I removed some of the query parameters and also hide the api key.

I have downloaded allow-control-allow-origin extension for chrome, and I have also changed the host file in: C:\Windows\System32\drivers\etc\hosts, so now I am accessing my application through a test domain: http://testdomain.com:3000/

But when I try to get the result, I get the following error:

POST http://partners.api.skyscanner.net/apiservices/pricing/v1.0/ 405 (Method Not Allowed)

When I go to NETWORK TAB and I select XHR, I see v1.0/ partners.api.skyscanner.net/apiservices/pricing which is red and under the headers I can see:

General Response Headers Request Headers Form Data, which has (seems like) a flight with my parameters??

I am not sure what I have done wrong but from the documentation and everything else seems like I should be getting code 200 which is for success but I keep getting 405. Can someone explain to me what I'm doing wrong?

Upvotes: 1

Views: 1878

Answers (1)

cusmar
cusmar

Reputation: 1913

Try this, after loading the querystring library:

var querystring = require('querystring');

var data = {
    cabinclass: 'Economy',
    country: 'UK',
    currency: 'GBP',
    // ...
    apikey: 'apikey' // be sure your API key is correct
};

var authOptions = {
    method: 'POST',
    url: 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0',
    data: querystring.stringify(data),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    json: true
};

axios(authOptions)
    .then(function(response){
        console.log(response.data);
        console.log(response.status);

        axios.get(response.headers.location + '?apiKey=apikey&stops=0&duration=360&includeCarriers=ba;u2;af').then(function(response) {
            console.log(response);
        });
    })
    .catch(function(error){
      console.log(error);
    });

According to the doc, try to use http://partners.api.skyscanner.net/apiservices/pricing/v1.0 instead of http://partners.api.skyscanner.net/apiservices/pricing/v1.0/ (without /).

Upvotes: 2

Related Questions