Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios GET request not working

I'm using: Axios: 0.17.1 Node: 8.0.0

The following standard Node get works fine, but the Axios version does not. Any ideas why?

Node http:

    http
    .get(`${someUrl}`, response => {
        buildResponse(response).then(results => res.send(results));
    })
    .on('error', e => {
        console.error(`Got error: ${e.message}`);
    });

Axios:

axios
    .get(`${someUrl}`)
    .then(function(response) {
        buildResponse(response).then(results => res.send(results));
    })
    .catch(function(error) {
        handleError(error, res);
    });

I just get a 503 in the catch, with "Request failed with status code 503"

like image 823
Ben Taliadoros Avatar asked Nov 22 '17 14:11

Ben Taliadoros


People also ask

How do you make a GET request with Axios?

A GET request can be made with Axios to “get” data from a server. The HTTP get request is performed by calling axios. get() .

How does Axios get work?

Axios works by making HTTP requests with NodeJS and XMLHttpRequests on the browser. If the request was successful, you will receive a response with the data requested. If the request failed, you will get an error. You can also intercept the requests and responses and transform or modify them.

Is Axios better than fetch?

Axios has the ability to intercept HTTP requests. Fetch, by default, doesn't provide a way to intercept requests. Axios has built-in support for download progress. Fetch does not support upload progress.

What is HTTP GET request in Axios?

An HTTP GET request is used to request a specified resource from a server. These requests do not contain any payload with them, i.e., the request doesn’t have any content. Axios GET is the method to make HTTP GET requests using the Axios library.

How do I get the same GET request from react and Axios?

GET request using axios with React hooks This sends the same GET request from React using axios, but this version uses React hooks from a function component instead of lifecycle methods from a traditional React class component.

Does Axios work with Node JS?

Axios is isomorphic, which means it can run in the browser and Node.js with the same code. When used on the server side, it uses Node’s native http module, whereas, on the client side, it uses XMLHttpRequests. On the client side, Axios also supports protection against XSRF. What is the Axios GET method?

Is proxy support a deal breaker for Axios?

I'd like to start using axios over request-promise but proxy support is a deal breaker. I believe axios is trying to determine proto for proxy based on request url (it shouldn't as my proxy is http but the request url is https).


2 Answers

It seems that you can pass Proxy details to Axios FYI.

From the docs...

  // 'proxy' defines the hostname and port of the proxy server
  // Use `false` to disable proxies, ignoring environment variables.
  // `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
  // supplies credentials.
  // This will set an `Proxy-Authorization` header, overwriting any existing
  // `Proxy-Authorization` custom headers you have set using `headers`.
  proxy: {
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
  },
like image 79
curv Avatar answered Oct 09 '22 12:10

curv


The only thing that worked for me was unsetting the proxy:

delete process.env['http_proxy'];
delete process.env['HTTP_PROXY'];
delete process.env['https_proxy'];
delete process.env['HTTPS_PROXY'];

From: Socket hang up when using axios.get, but not when using https.get

like image 39
Ben Taliadoros Avatar answered Oct 09 '22 11:10

Ben Taliadoros