0

我觉得这应该是用 node.js 完成的一件简单的事情,但是响应总是以 400 错误的形式返回,这是我下面的代码,不需要调试太多。我开始怀疑 API 文档是否有问题。

const fetch = require("node-fetch");

const apiKey = 'APP-KEY';
const sessionId = 'SESSION-KEY'
const url = "https://api.betfair.com/exchange/betting/json-rpc/v1"

async function listEventTypes()
{
    const body = '[{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listEventTypes", "params": {"filter":{}}, "id": 1}]';
    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'X-Application' : apiKey,
            'X-Authentication' : sessionId,
            'Accept' : 'application/json',
            'Content-type' : 'application/json'
        },
        data: body          
    }).then(response => {
    if (response.ok) {
      return response.json();
    }
    throw new Error('Request failed!');
  }, networkError => {
    console.log(networkError.message)
  })
}


function runTheApp()
{   
    console.log('Starting the Betfair Horse Racing API App!\n');
    console.log(listEventTypes());
}

runTheApp();
4

0 回答 0