将单项GET请求转换为axios

时间:2019-08-11 18:55:41

标签: javascript reactjs api axios next.js

我正在使用TheRundown API来获取体育数据,例如他们给我的示例使用unirest,我正在尝试将该示例转换为axios请求。我大部分时间都可以使用它,但是我遇到的问题是axios请求中包含query参数。

我尝试将params与axios请求一起使用,但是它并没有为我提供我所要求的JSON额外数据。

这是我尝试转换为axios的示例代码:

var unirest = require("unirest");

var req = unirest("GET", "https://therundown-therundown-v1.p.rapidapi.com/sports/3/events");

req.query({
    "include": [
        "all_periods",
        "scores"
    ]
});

req.headers({
    "x-rapidapi-host": "therundown-therundown-v1.p.rapidapi.com",
    "x-rapidapi-key": process.env.THERUNDOWN_API_KEY
});


req.end(function (res) {
    if (res.error) throw new Error(res.error);

    console.log(res.body);
});

这是我到目前为止通过axios获得的代码:

response = await axios({
  method: "GET",
  url: `https://therundown-therundown-v1.p.rapidapi.com/sports/1/events`,
  headers: {
    "x-rapidapi-host": "therundown-therundown-v1.p.rapidapi.com",
    "x-rapidapi-key": process.env.THERUNDOWN_API_KEY
  },
  params: {
    include: ["all_periods", "scores"]
  }
}).then(
  function(response) {
    sportsData[0].data = response.data;
  }.bind(this)
);

因此,我的axios请求中缺少的数据是我的axios请求中的"score": [Object]。您可以从下面发布的JSON数据中看到。

这是我从unirest 'GET'请求中获得的JSON:

{ meta: { delta_last_id: '11e9-bc68-68d68d53-8563-74040f62bb04' },
  events:
   [ { event_id: '402cb5659e1cbf08d6f6648fffcd6f91',
       sport_id: 3,
       event_date: '2019-08-11T20:10:00Z',
       rotation_number_away: 927,
       rotation_number_home: 928,
       score: [Object],
       teams: [Array],
       teams_normalized: [Array],
       line_periods: [Object] },
     { event_id: '4cde94df721e1c18bba6389c0f80b34b',
       sport_id: 3,
       event_date: '2019-08-11T23:05:00Z',
       rotation_number_away: 911,
       rotation_number_home: 912,
       score: [Object],
       teams: [Array],
       teams_normalized: [Array],
       line_periods: [Object] },
     { event_id: 'cbfba54a01f82b54dbc4e417cf2e371b',
       sport_id: 3,
       event_date: '2019-08-11T20:10:00Z',
       rotation_number_away: 913,
       rotation_number_home: 914,
       score: [Object],
       teams: [Array],
       teams_normalized: [Array],
       line_periods: [Object] },
     { event_id: 'd2f689438cc61fdebadd464b40247f4a',
       sport_id: 3,
       event_date: '2019-08-11T19:40:00Z',
       rotation_number_away: 909,
       rotation_number_home: 910,
       score: [Object],
       teams: [Array],
       teams_normalized: [Array],
       line_periods: [Object] } ] }

这是我从axios 'GET'请求中获得的JSON:

{ meta: { delta_last_id: '11e9-bc68-68d68d53-8563-74040f62bb04' },
  events:
   [ { event_id: '402cb5659e1cbf08d6f6648fffcd6f91',
       sport_id: 3,
       event_date: '2019-08-11T20:10:00Z',
       rotation_number_away: 927,
       rotation_number_home: 928,
       teams: [Array],
       teams_normalized: [Array],
       lines: [Object] },
     { event_id: '4cde94df721e1c18bba6389c0f80b34b',
       sport_id: 3,
       event_date: '2019-08-11T23:05:00Z',
       rotation_number_away: 911,
       rotation_number_home: 912,
       teams: [Array],
       teams_normalized: [Array],
       lines: [Object] },
     { event_id: 'cbfba54a01f82b54dbc4e417cf2e371b',
       sport_id: 3,
       event_date: '2019-08-11T20:10:00Z',
       rotation_number_away: 913,
       rotation_number_home: 914,
       teams: [Array],
       teams_normalized: [Array],
       lines: [Object] },
     { event_id: 'd2f689438cc61fdebadd464b40247f4a',
       sport_id: 3,
       event_date: '2019-08-11T19:40:00Z',
       rotation_number_away: 909,
       rotation_number_home: 910,
       teams: [Array],
       teams_normalized: [Array],
       lines: [Object] } ] }

1 个答案:

答案 0 :(得分:0)

Axios默认使用arrayFormat: brackets,并且api希望重复使用。

当前的axios端点:

https://therundown-therundown-v1.p.rapidapi.com/sports/3/events?include[]=all_periods&include[]=scores

正确的端点:

https://therundown-therundown-v1.p.rapidapi.com/sports/3/events?include=all_periods&include=scores

您可以通过使用查询字符串包arrayFormat重复项提供paramsSerializer来更改此行为。

var qs = require('qs');

response = await axios({
  method: "GET",
  url: `https://therundown-therundown-v1.p.rapidapi.com/sports/1/events`,
  headers: {
    "x-rapidapi-host": "therundown-therundown-v1.p.rapidapi.com",
    "x-rapidapi-key": process.env.THERUNDOWN_API_KEY
  },
  params: {
    include: ["all_periods", "scores"]
  },
  paramsSerializer: function(params) {
       return qs.stringify(params, {arrayFormat: 'repeat'})
  },
}).then(
  function(response) {
    sportsData[0].data = response.data;
  }.bind(this)
);