创建会话时SkyScanner 415状态码

时间:2016-11-18 07:10:23

标签: node.js http-post skyscanner

我正在尝试在我的Node JS应用程序上创建一个这样的会话:

import * as request from 'request';
const apiKey = '123123123123123123';
const urlApi = 'http://partners.api.skyscanner.net/apiservices/pricing/v1.0?apikey=' + apiKey;

const headers = {
                    'Accept': 'application/json',
                    'Content-Type': 'application/x-www-form-urlencoded'
                };

var options = {
                url: urlApi,
                method: 'POST',
                headers: headers,
                data: {
                    country: 'UK',
                    currency: 'GBP',
                    locale: 'en-GB',
                    locationSchema: 'iata',
                    apikey: '123123123123123123',
                    grouppricing: 'on',
                    originplace: 'EDI',
                    destinationplace: 'LHR',
                    outbounddate: '2016-12-29',
                    inbounddate: '2016-12-05',
                    adults: 1,
                    children: 0,
                    infants: 0,
                    cabinclass: 'Economy'
                }
};

request.post(options, function(error: Error, response: any, body: any)
{
    if (!error && response.statusCode === 415) {
        console.log('Error: ' + response.statusCode);
    }
    else {
        console.log(response.statusCode);
    }
});

但是,它总是返回statusCode 415.我正在关注这里的文档https://support.business.skyscanner.net/hc/en-us/articles/211308489-Flights-Live-Pricing但是到目前为止我还没有运气......

2 个答案:

答案 0 :(得分:1)

我解决了实际上您在请求参数中使用 body 的问题,您应该使用form而不是将值附加为表单,而不是Stringified JSON或Serialized查询参数

var apiKey = "1234567897avasd85asd1a5dasd5a";
request.post("http://partners.api.skyscanner.net/apiservices/pricing/v1.0?apiKey=" + apiKey, {
    form :{   // you were using body here use form instead
        country: 'UK',
        currency: 'GBP',
        locale: 'en-GB',
        locationSchema: 'iata',
        apikey: apiKey,
        grouppricing: 'on',
        originplace: 'EDI',
        destinationplace: 'LHR',
        outbounddate: '2018-08-09',
        inbounddate: '2018-08-29',
        adults: "1",
        children: "0",
        infants: "0",
        cabinclass: 'Economy'
    },
    headers: {
        'content-type': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
},function(error,response){
   if(!error){
    console.log(response.headers.location);
   }else{
    console.log("still got errors");
   }
});

答案 1 :(得分:0)

在我发布问题后,我找到了错过请求的原因。我在url中使用了查询,但你需要通过用body替换属性数据将它们放在post体中。我猜你还需要在请求选项中使用json:true。

考虑https://github.com/request/request#requestoptions-callback