node.jsaxiosrequest-promise

How to convert request-promise get Request to Axios get Request


This is my request-promise get Request Code I want to convert this code to Axios get Request

let file = rp.get({
  uri: "url",
  headers: {
    "Accept": "message/rfc2822"
  }
}).auth("api", "ap`enter code here`i-key")
/**Access the buffer here**/
file.on('data', (data => {
  console.log("data", data)
  // Here I get blob format data
}))

Solution

  • Axios code of your code will be:

    axios.get("url", {
            headers: {
                Accept: "message/rfc2822"
            },
            auth: {
                username: "api",
                password: "api-key"
            },
            responseType: "stream"
        })
        .then(response => {
            response.data.pipe(concat(data => {
                console.log("data", data);
            }));
        })
        .catch(error => {
            console.error(error);
        });