0

I am trying to send a request to the particle cloud from a NodeJS application. I am using Axios to make the PUT request. The application sends the request through a proxy server which is configured as well.

// axios proxy - not working
axios.default.put("https://api.particle.io/v1/devices/<deviceId>/ping", {}, {
proxy: {host: <proxy_ip>, protocol:'http', port:<port_no>},
headers: {
    authorization: "Bearer <access_token>"
}
}).then((response) => {
    console.log("Success", response.data);
}).catch((error) => {
   console.log("Failed", error);
});

Error Message: Request failed with status code 400

When I send this request I get a 400 Bad Request response from the particle cloud. But when I send the same request using the request module of NodeJS, the request is successful.

var options = {
   method: 'PUT',
   url: 'https://api.particle.io/v1/devices/<device_id>/ping',
   proxy: {hostname: <proxy_ip>, protocol:'http', port:<port_no>},
   headers: 
   { 
       authorization: 'Bearer <access_token>'
   },
   form: false
};
request(options, function (error, response, body) {
   if (error) throw new Error(error);
   console.log(response);
});

Response: body: '{"online":false,"ok":true}'

The request also works when the application was deployed on the open network and axios was used without the proxy configuration.

// axios without proxy - working
axios.default.put("https://api.particle.io/v1/devices/<deviceId>/ping", {}, {
    headers: {
        authorization: "Bearer <access_token>"
    }
}).then((response) => {
    console.log("Success", response.data);
}).catch((error) => {
    console.log("Failed", error);
});

Questions:

  1. Why is the request from Axios failing with proxy configuration?
  2. Is this an inherent issue with Axios?

Regards.

1 Answers1

3

Axios itself has a bug which isnt fixed yet.

To overcome this issue, https-proxy-agent can be used instead of axios proxy.

const HttpsProxyAgent = require('https-proxy-agent')

axios.default.put("https://api.particle.io/v1/devices/<deviceId>/ping", {}, {
    headers: {
        authorization: "Bearer <access_token>"
    }, 
    proxy: false,
    httpsAgent: new HttpsProxyAgent('http://proxy_domain:port')
}).then((response) => {
    console.log("Success", response.data);
}).catch((error) => {
    console.log("Failed", error);
});
Ganeshkumar K
  • 485
  • 2
  • 8