Download binary file with Axios

I was able to create a workable gist (without using FileSaver) as below: axios.get(“http://my-server:8080/reports/my-sample-report/”, { responseType: ‘arraybuffer’, headers: { ‘Content-Type’: ‘application/json’, ‘Accept’: ‘application/pdf’ } }) .then((response) => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement(‘a’); link.href = url; link.setAttribute(‘download’, ‘file.pdf’); //or any other extension document.body.appendChild(link); link.click(); }) .catch((error) => console.log(error));

Force download GET request using axios

You’re getting empty PDF ’cause no data is passed to the server. You can try passing data using data object like this axios .post(`order-results/${id}/export-pdf`, { data: { firstName: ‘Fred’ }, responseType: ‘arraybuffer’ }) .then(response => { console.log(response) let blob = new Blob([response.data], { type: ‘application/pdf’ }), url = window.URL.createObjectURL(blob) window.open(url) // Mostly the same, I … Read more

How to prevent Axios from encoding my request parameters?

You can use a custom param serializer as follows: axios.get(‘https://foobar.com/api’, { paramsSerializer: function(params) { var result=””; // Build the query string return result; } }); paramsSerializer can be set at the instance level: var instance = axios.create({ paramsSerializer: function(params) { /* … */ } }) or at the global level: axios.defaults.paramsSerializer = function(params) { /* … Read more

Axios. How to get error response even when api return 404 error, in try catch finally

According to the documentation, the full response is available as a response property on the error. So I’d use that information in the catch block: (async() => { let apiRes = null; try { apiRes = await axios.get(‘https://silex.edgeprop.my/api/v1/a’); } catch (err) { console.error(“Error response:”); console.error(err.response.data); // *** console.error(err.response.status); // *** console.error(err.response.headers); // *** } finally … Read more

React: Axios Network Error

If Creating an API Using NodeJS Your Express app needs to use CORS (Cross-Origin Resource Sharing). Add the following to your server file: // This should already be declared in your API file var app = express(); // ADD THIS var cors = require(‘cors’); app.use(cors()); For fuller understanding of CORS, please read the Mozilla Documentation … Read more

Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?

Try this out! axios.get(‘your_url’, {withCredentials: true}); //for GET axios.post(‘your_url’, data, {withCredentials: true}); //for POST axios.put(‘your_url’, data, {withCredentials: true}); //for PUT axios.delete(‘your_url’, data, {withCredentials: true}); //for DELETE For more information on this from the axios docs: “withCredentials indicates whether or not cross-site Access-Control requests should be made using credentials” – https://github.com/axios/axios More detail on withCredentials: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials