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

Time:01-31

I'm trying o retrieve information from an url. If I make it from Insomnia works fine:

enter image description here

However, when I try to do this with my code, I've got this error message:

<?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'
}

And this is my code:

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;
};

With this same code I have been retriving information from the same company but from other end points.

What happend? Whan am I doing wrong?

Edit I:

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);
    });
});

CodePudding user response:

Your question is lacking the information needed to troubleshoot this. However, here is something you can try to get more insight: You can use a proxy/proxy service like Beeceptor and call it with both Insomnia and your code.

You can then analyze the differences between what is received by the proxy in both cases and see what is wrong.

From looking at the code you published in your question, it is likely the URL and/or payload (i.e. data) is causing the endpoint to return a 404.

Note: you are asking for a JSON reply (Accept: application/json) but the endpoint replies with an XML ('content-type': 'text/xml; charset=UTF-8'), are you sure the API supports JSON response?

  •  Tags:  
  • Related