anubhav adarsh
anubhav adarsh

Reputation: 41

why making a get request with axios failing with status code 503?

const { default: Axios } = require("axios");
var giveMeAJoke = require("give-me-a-joke");

const getjoke = async () => {
  try {
    const data1 = await Axios.get("https://icanhazdadjoke.com", {
      headers: {
        Accept: "application/json",
      },
    });
  } catch (e) {
    console.log(e);
  }
};

getjoke();

This is what I was trying with axios. I made the same request with fetch() and it worked fine. There are few packages I am using that are dependent on axios and every package is throwing the very same error. Help me out.

node index.js 
Error: Request failed with status code 503
    at createError (D:\web-technology\practice\test\jokester\node_modules\axios\lib\core\createError.js:16:15)
    at settle (D:\web-technology\practice\test\jokester\node_modules\axios\lib\core\settle.js:17:12)
    at IncomingMessage.handleStreamEnd (D:\web-technology\practice\test\jokester\node_modules\axios\lib\adapters\http.js:236:11)
    at IncomingMessage.emit (node:events:406:35)
    at endReadableNT (node:internal/streams/readable:1329:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Upvotes: 0

Views: 19018

Answers (1)

Rahul Kumar
Rahul Kumar

Reputation: 3157

It seems custom User-Agent header is required by server and the default value in case of axios ("axios/0.21.1") creates problem because of axios followed by / symbol, so if you give some other value or remove / character eg. ("axios 0.21.1"), then it works.

const axios = require('axios').default;

const getjoke = async () => {
  try {
    const response = await axios.get("https://icanhazdadjoke.com/", {
      headers: {
        Accept: "application/json",
        "User-Agent": "axios 0.21.1"
      }
    });
    console.log("received response: ", response.data);
  } catch (err) {
    console.log("received error: ", err.toJSON());
  }
};

getjoke();

Output:

received response: {
  id: 'm3wPZoz51ob',
  joke: 'Did you hear the news? FedEx and UPS are merging. They’re going to go by the name Fed-Up from now on.',
  status: 200
}

Upvotes: 3

Related Questions