反应本机发送头失败

时间:2020-02-27 15:58:57

标签: react-native fetch

我正在尝试访问

https://developer-portal.driver-vehicle-licensing.api.gov.uk/apis/vehicle-enquiry-service/vehicle-enquiry-service-description.html#usage

但是,我一直在丢失身份验证令牌错误。似乎身份验证令牌未通过标头发送。以下是我正在使用的代码。我想念什么?

var headers = {
            Accept: 'application/json',
            'Content-Type': 'application/json',
            'x-api-key' : API.vehicleApiKey,
            'X-API-KEY' : API.vehicleApiKey,
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": "X-API-KEY, x-api-key, Origin, Content-Type, Accept, Access-Control-Request-Method",
            "Access-Control-Allow-Methods": "POST"
        };

        fetch(API.vehicleApiUrl + "/vehicles", {
            method: 'POST',
            headers: headers,
            body: "{\"registrationNumber\": \"ABC1234\"}",
        })
        .then(response => response.json())
        .then(response => {       
            console.log(response);   
        })
        .catch(error => {
          console.log(error);
          //this.hideLoading();
        });

1 个答案:

答案 0 :(得分:0)

您应首先register,并获得应用程序API密钥和类似的标题

var headers = {
            'name': x-api-key,
            'value' : value_from_api_key,
            'body': {"registrationNumber": "ABC1234"}
        };