Getting 400 error code when I run axios get request?

Arfan Jihad

I write some code to getting info

const stock = await Stock.find({
    exchange: exchange
});
// Here stock array length is 5300

stock.forEach(async (stockEl) => {
    const EOD_API = process.env.EOD_HISTORICAL_API
    const {data} = await axios.get(`https://eodhistoricaldata.com/api/fundamentals/${stockEl.code}?api_token=${EOD_API}&filter=General::Industry`);
    console.log(data);
});

Here I place get request for every stock array element by forEach function. Then it give me error like image- Click to see images

But When I place it outside of forEach function like this-

const EOD_API = process.env.EOD_HISTORICAL_API
const {data} = await axios.get(`https://eodhistoricaldata.com/api/fundamentals/${stockEl.code}?api_token=${EOD_API}&filter=General::Industry`);
console.log(data);

Then it gives no error. For Remembering Stock has 5300 element, that means axios run 5300 times.

Any solution or idea?

Adam Morsi

You need to make a few changes:

  • Replace forEach with for because forEach is not promise aware

  • Use try, catch => catch any errors

  • Use Promise.allSettled => it allows you to run all promisses together without waiting each other which in return will enhance your app performance. It returns an array with status ("fulfilled", "rejected")

    const fetchSingleStockElement = async (stockEl) => {
          try {
              const EOD_API = process.env.EOD_HISTORICAL_API,
                  { data } = await axios(
    
    `https://eodhistoricaldata.com/api/fundamentals/${stockEl.code}?api_token=${EOD_API}&filter=General::Industry`
                  );
              return data;
          } catch (err) {
              throw new Error(err);
          }
      };
    
      const fetchAllStockData = async () => {
          let promisesArray = [];
          try {
              //fetch stock array
              const { data } = await Stock.find({
                  exchange: exchange
              });
    
              //fetch single stock
              for (let i = 0; i < data.length; i++) {
                  promisesArray.push(fetchSingleStockElement(data[i].id));
              }
    
              const results = await Promise.allSettled(promisesArray);
              console.log('results', results);
          } catch (err) {
              console.log('results error', err);
          }
    

    };

Here is a working example with fake API of 4466 entries:

const fetchSingleAirline = async (airlineId) => {
        try {
            const { data } = await axios(`https://api.instantwebtools.net/v1/airlines/${airlineId}`);
            return data;
        } catch (err) {
            throw new Error(err);
        }
    };

const fetchAllAirlineData = async () => {
    let promisesArray = [];
    try {
        const { data } = await axios('https://api.instantwebtools.net/v1/airlines');
        for (let i = 0; i < data.length; i++) {
            promisesArray.push(fetchSingleAirline(data[i].id));
        }

        const results = await Promise.allSettled(promisesArray);
        console.log('results', results);
    } catch (err) {
        console.log('results error', err);
    }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

getting Network Error when calling axios get request in react js

getting error 400 with axios post request & undefined error

Why do I keep getting a 400 Bad Request Error when exchanging code for access token from Spotify API?

When I try to install Pycharm or Atom through Ubuntu software centre, I get the error: snapd returned status code 400: Bad Request

i am getting 'cb' argument error when i run the code

I want to get request from an api url using fetch method. But I keep on getting error 400

Getting 400 Bad Request error with Python Socket GET request

When I'm using URLEncoder for Http GET Request I'm getting proper URL but when I push that code to live or on server I'm getting error

GET 400 and Uncaught (in promise) Error: Request failed with status code 400

why am i getting a 400 (bad request) error when using ajax?

Error when trying to authorize Axios get request

Vuejs with Axios - getting ''cross-origin" error when using get request

Getting 415 error when trying to pass in multiple arguments to a get request in React with axios

When I run the code, I get an error the guesses is not defined

POST request getting 400 error code with pydev Eclipse but working on Postman

Getting 400 from Axios Post request

Why do I get BioPython HTTPError: HTTP Error 400: Bad Request when I use Esearch and Efetch

Why do I get a Error 400 when I want to post a JSON file HTTP REQUEST

Why I get a "Bad Request" Error 400 when I try to connect with Api with fetch?

400 Bad Request using Axios get request

Error 400 Bad request when getting Open Weather API (geolocation)

Why am I getting the 400 error (Bad request)

I keep getting HTTP Error 400: Bad Request from urlopen

Django: Why am I getting a 400 bad request error?

Getting Request failed with status code 403 with axios get

Getting status code 304 on a get request with axios using react and redux

Getting 400 response when i try for POST request

Getting a '400 Bad Request' when I try to link API

axios get request Error: Request failed with status code 504