无法从 URL 检索数据,但我之前已经从另一个端点检索数据

Could not retrieve data from URL when I have done it before but from another end point

我正在尝试从 url 检索信息。如果我从 Insomnia 做到这一点,效果很好:

但是,当我尝试用我的代码执行此操作时,我收到了以下错误消息:

<?xml version="1.0" encoding="UTF-8"?>
<error><code>404</code><description>could not retrieve data. please check request.</description></error>

status: 404
{
  server: 'nginx',
  date: 'Fri, 21 Jan 2022 17:15:12 GMT',
  'content-type': 'text/xml; charset=UTF-8',
  'transfer-encoding': 'chunked',
  connection: 'close'
}

这是我的代码:

const getData = async (url_api, data) => {    

    console.log(data);
    let headers = {
            "Accept"        :   "application/json",
            "content-type"  :   "application/json",
            "Authorization" :   config.giata.authorization
    };

    let result = await axios.post(
        url_api, 
        data,
        {headers: headers})
    .then((response) => {
        //console.log(response.data);
        let result = new Result("OK");
        result.set_status(response.status);
        result.set_data(response.data);
        return result;
    }).catch((error) => {
        let result = null;
        if (error.response){
            // The request was made and the server responded with a status code that falls out of the range of 2xx            
            console.log(`data: ${error.response.data}`);
            console.log(`status: ${error.response.status}`);
            console.log(error.response.headers);
            result = new Result("KO");
            result.set_data(error.response.data);
            result.set_status(error.response.status);
            result.set_headers(error.response.headers);
        }else if (error.request){
            // The request was made but no response was received `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js     
            result = new Result("KO");
            result.set_request(error.request);
            // console.log(error.request);
        }else{
            // Something happened in setting up the request that triggered an Error
            result = new Result("KO");
            result.set_message(error.message);
            // console.log(error.message);
        }
        return result;
    });

    return result;
};

使用相同的代码,我一直在从同一家公司但从其他端点检索信息。

发生什么事了?我哪里做错了?

编辑我:

import {Result} from "../com/result";
import axios from "axios";


describe("Load pictures and descriptions for each property", () => {
    describe("Request data from server", () => {
        test("Test 100: Load data from server", async () => {

            let headers = {
                "Accept"        :   "text/xml; charset=UTF-8",
                "content-type"  :   "text/xml; charset=UTF-8",
                "Authorization": "Basic XXXXXXXXXXXXXXXXXXXXXX="
            };
            let url_api = "http://ghgml.giatamedia.com/webservice/rest/1.0/items/2275";
            let data = {};

            let result = await axios.post(
                url_api, 
                data,
                {headers: headers})
            .then((response) => {
                //console.log(response.data);
                let result = new Result("OK");
                result.set_status(response.status);
                result.set_data(response.data);
                return result;
            }).catch((error) => {
                let result = null;
                if (error.response){
                    // The request was made and the server responded with a status code that falls out of the range of 2xx            
        /*             console.log(`data: ${error.response.data}`);
                    console.log(`status: ${error.response.status}`);
                    console.log(error.response.headers); */
                    result = new Result("KO");
                    result.set_data(error.response.data);
                    result.set_status(error.response.status);
                    result.set_headers(error.response.headers);
                }else if (error.request){
                    // The request was made but no response was received `error.request` is an instance of XMLHttpRequest in the browser and an instance of
                    // http.ClientRequest in node.js     
                    result = new Result("KO");
                    result.set_request(error.request);
                    // console.log(error.request);
                }else{
                    // Something happened in setting up the request that triggered an Error
                    result = new Result("KO");
                    result.set_message(error.message);
                    // console.log(error.message);
                }
                return result;
            });
            console.log(result);
        }, 10000);
    });
});

您的问题缺少解决此问题所需的信息。 但是,您可以尝试通过以下方式获得更多洞察力: 您可以使用像 Beeceptor 这样的 proxy/proxy 服务,并使用 Insomnia 和您的代码调用它。

然后您可以分析代理在两种情况下收到的内容之间的差异,看看哪里出了问题。

通过查看您在问题中发布的代码,很可能是 URL and/or 有效负载(即数据)导致端点 return 404。

注意:您要求 JSON 回复 (Accept: application/json) 但端点回复 XML ('content-type': 'text/xml; charset=UTF-8'),您确定 API 支持 JSON 响应吗?