Questions tagged [axios]
Axios is a Promise-based HTTP client for JavaScript which can be used in your front-end application and in your Node.js backend.
axios
24,751
questions
587
votes
16
answers
790k
views
How can I get the status code from an HTTP error in Axios?
This may seem stupid, but I'm trying to get the error data when a request fails in Axios.
axios
.get('foo.example')
.then((response) => {})
.catch((error) => {
console.log(error); //...
459
votes
21
answers
1.2m
views
axios post request to send form data
axios POST request is hitting the url on the controller but setting null values to my POJO class, when I go through developer tools in chrome, the payload contains data. What am I doing wrong?
Axios ...
453
votes
16
answers
1.2m
views
How to set header and options in axios?
I use Axios to perform an HTTP post like this:
import axios from 'axios'
params = {'HTTP_CONTENT_LANGUAGE': self.language}
headers = {'header1': value}
axios.post(url, params, headers)
Is this ...
344
votes
12
answers
208k
views
What is difference between Axios and Fetch? [closed]
I'm trying to call a web service for my application, and there are two options available: Fetch and Axios. I'm not sure which one to choose, so I'm looking for information to help me decide. Can you ...
342
votes
15
answers
878k
views
Axios handling errors
I'm trying to understand javascript promises better with Axios. What I pretend is to handle all errors in Request.js and only call the request function from anywhere without having to use catch().
In ...
339
votes
23
answers
701k
views
Make Axios send cookies in its requests automatically
I am sending requests from the client to my Express.js server using Axios.
I set a cookie on the client and I want to read that cookie from all Axios requests without adding them manually to request ...
337
votes
17
answers
305k
views
Axios get access to response header fields
I'm building a frontend app with React and Redux and I'm using axios to perform my requests. I would like to get access to all the fields in the header of the response. In my browser I can inspect the ...
331
votes
19
answers
715k
views
Sending the bearer token with axios
In my react app i am using axios to perform the REST api requests.
But it's unable to send the Authorization header with the request.
Here is my code:
tokenPayload() {
let config = {
headers: ...
326
votes
10
answers
808k
views
Passing headers with axios POST request
I have written an Axios POST request as recommended from the npm package documentation like:
var data = {
'key1': 'val1',
'key2': 'val2'
}
axios.post(Helper.getUserAPI(), data)
.then((...
320
votes
9
answers
486k
views
Attach Authorization header for all axios requests
I have a react/redux application that fetches a token from an api server. After the user authenticates I'd like to make all axios requests have that token as an Authorization header without having to ...
316
votes
10
answers
647k
views
How to post a file from a form with Axios
Using raw HTML when I post a file to a flask server using the following I can access files from the flask request global:
<form id="uploadForm" action='upload_file' role="form" method="post" ...
291
votes
20
answers
544k
views
Axios - DELETE Request With Request Body and Headers?
I'm using Axios while programming in ReactJS and I pretend to send a DELETE request to my server.
To do so I need the headers:
headers: {
'Authorization': ...
}
and the body is composed of
var ...
288
votes
20
answers
641k
views
How to download files using axios
I am using axios for basic http requests like GET and POST, and it works well. Now I need to be able to download Excel files too. Is this possible with axios? If so does anyone have some sample code? ...
273
votes
10
answers
484k
views
How to send Basic Auth with axios
I'm trying to implement the following code, but something is not working. Here is the code:
var session_url = 'http://api_address/api/session_endpoint';
var username = 'user';
var password = 'password'...
255
votes
4
answers
450k
views
Axios get in url works but with second parameter as object it doesn't
I'm trying to send GET request as second parameter but it doesn't work while it does as url.
This works, $_GET['naam'] returns test:
export function saveScore(naam, score) {
return function (...
210
votes
4
answers
475k
views
How to post query parameters with Axios?
I am trying to post on an API with some query params.
This is working on PostMan / Insomnia when I am trying to by passing mail and firstname as query parameters :
http://localhost:8000/api/mails/...
184
votes
7
answers
295k
views
How can you use axios interceptors?
I have seen axios documentation, but all it says is
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
return config;
}, ...
184
votes
3
answers
331k
views
How to return values from async functions using async-await from function? [duplicate]
How can I return the value from an async function?
I tried to like this
const axios = require('axios');
async function getData() {
const data = await axios.get('https://jsonplaceholder.typicode....
170
votes
6
answers
169k
views
How to log all axios calls from one place in code
I am using axios for a react application and I would like to log all axios calls that I'm making anywhere in the app. I'm already using a single global instance of axios via the create function and I ...
167
votes
7
answers
324k
views
How do I test axios in Jest?
I have this action in React:
export function fetchPosts() {
const request = axios.get(`${WORDPRESS_URL}`);
return {
type: FETCH_POSTS,
payload: request
}
}
How do I test ...
154
votes
6
answers
211k
views
Download an image using Axios and convert it to base64
I need to download a .jpg image from a remote server and convert it into a base64 format. I'm using axios as my HTTP client. I've tried issuing a git request to the server and checking the response....
153
votes
12
answers
380k
views
How to send authorization header with axios
How can I send an authentication header with a token via axios.js?
I have tried a few things without success, for example:
const header = `Authorization: Bearer ${token}`;
return axios.get(...
150
votes
12
answers
989k
views
Axios having CORS issue
I added proxy in package.json and it worked great, but after npm run build the CORS issue has resurfaced again, does anyone know how to deal with CORS issue after npm run build in React.
I have tried ...
149
votes
20
answers
684k
views
Access Control Origin Header error using Axios
I'm making an API call using Axios in a React Web app. However, I'm getting this error in Chrome:
XMLHttpRequest cannot load
https://example.restdb.io/rest/mock-data. No
'Access-Control-Allow-Origin' ...
138
votes
14
answers
177k
views
Vue JS returns [__ob__: Observer] data instead of my array of objects
I've created a page where I want to get all my data from the database with an API call, but I'm kinda new to VueJS and Javascript aswell and I don't know where I'm getting it wrong. I did test it with ...
136
votes
10
answers
245k
views
how to cancel/abort ajax request in axios
I use axios for ajax requests and reactJS + flux for render UI. In my app there is third side timeline (reactJS component). Timeline can be managed by mouse's scroll. App sends ajax request for the ...
134
votes
11
answers
461k
views
Returning data from Axios API [duplicate]
I am trying to use a Node.JS application to make and receive API requests. It does a get request to another server using Axios with data it receives from an API call it receives. The second snippet is ...
133
votes
9
answers
408k
views
How to configure axios to use SSL certificate?
I'm trying to make a request with axios to an api endpoint and I'm getting the following error: Error: unable to verify the first certificate
It seems the https module, which axios uses, is unable to ...
132
votes
12
answers
702k
views
Has been blocked by CORS policy: Response to preflight request doesn’t pass access control check
I have created trip server. It works fine and we are able to make POST request by Insomnia but when we make POST request by axios on our front-end, it sends an error:
has been blocked by CORS policy: ...
128
votes
19
answers
206k
views
How to correctly use axios params with arrays
How to add indexes to array in query string?
I tried send data like this:
axios.get('/myController/myAction', { params: { storeIds: [1,2,3] })
And I got this url:
http://localhost/api/myController/...
127
votes
17
answers
102k
views
"Cannot use import statement outside a module" with Axios
I have a Vue.js application where two files contain:
import axios from "axios"
These files are located in src/lib within the application and include the import statement on their first line.
...
120
votes
8
answers
141k
views
catching error body using axios post
I am sending a status code 422 from my backend code with response body which contains the description of the error. I am using axios post as below to post a request:
post: function(url, reqBody) {
...
110
votes
5
answers
173k
views
Axios Http client - How to construct Http Post url with form params
I am trying to create a postHTTP request with some form parameters that are to be set. I am using the axios with node server. I already have a java code implementation of constructing a url as given ...
108
votes
14
answers
472k
views
How can I add raw data body to an axios request?
I am trying to communicate with an API from my React application using Axios. I managed to get the GET request working, but now I need a POST one.
I need the body to be raw text, as I will write an ...
106
votes
13
answers
247k
views
Python: FastAPI error 422 with POST request when sending JSON data
I'm building a simple API to test a database. When I use GET request everything works fine, but if I change to POST, I get 422 Unprocessable Entity error.
Here is the FastAPI code:
from fastapi import ...
106
votes
7
answers
334k
views
Change the default base url for axios
I have configured my axios like this
const axiosConfig = {
baseURL: 'http://127.0.0.1:8000/api',
timeout: 30000,
};
Vue.prototype.$axios = axios.create(axiosConfig)
Inside my component, I make a ...
105
votes
3
answers
128k
views
Axios Interceptors retry original request and access original promise
I have an interceptor in place to catch 401 errors if the access token expires. If it expires it tries the refresh token to get a new access token. If any other calls are made during this time they ...
104
votes
3
answers
238k
views
How do I set multipart in axios with react?
When I curl something, it works fine:
curl -L -i -H 'x-device-id: abc' -F "url=http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" http://example.com/upload
How do I get this to work right with ...
103
votes
8
answers
141k
views
failed to load response data request content was evicted from inspector cache
i am trying to get file from server that has size of(15mb) through Axios Request. showing status "200", but not sending file previewData(base64) and showing this error "failed to load ...
101
votes
6
answers
249k
views
How to handle 401 (Authentication Error) in axios and react?
I have one file request.js which contains wrapper for axios ajax request. I am calling request function from multiple react components and when one of the request fails I want to refresh the token and ...
95
votes
7
answers
105k
views
Multiple fields with same key in query params (axios request)?
So the backend (not under my control) requires a query string like this:
http://example.com/?foo=5&foo=2&foo=11
But axios uses a JS object to send the request params:
axios.get('http://...
93
votes
2
answers
307k
views
React and TypeScript—which types for an Axios response?
I am trying to present a simple user list from an API which returns this:
[{"UserID":2,"FirstName":"User2"},{"UserID":1,"FirstName":"User1"}]...
91
votes
13
answers
196k
views
node.js axios download file stream and writeFile
i want download a pdf file with axios and save on disk (server side) with fs.writeFile, i have tried:
axios.get('https://xxx/my.pdf', {responseType: 'blob'}).then(response => {
fs.writeFile('/...
90
votes
20
answers
459k
views
React: Axios Network Error
This is my first time using axios and I have encountered an error.
axios.get(
`http://someurl.com/page1?param1=1¶m2=${param2_id}`
)
.then(function(response) {
alert();
})
....
88
votes
9
answers
91k
views
CORS with Azure function from localhost (not CLI)
We are using axios in a vue.js app to access an Azure function. Right now we are getting this error:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://...
87
votes
8
answers
238k
views
Timeout feature in the axios library is not working
I have set axios.defaults.timeout = 1000;
I stopped the server that provides me with the APIs.
But it takes more than 1s to timeout after sending a request.
This is how my request looks:
import ...
87
votes
1
answer
315k
views
Send object with axios get request [duplicate]
I want to send a get request with an object. The object data will be used on the server to update session data. But the object doesn't seem to be sent correctly, because if I try to send it back to ...
86
votes
8
answers
137k
views
Axios: chaining multiple API requests
I need to chain a few API requests from the Google Maps API, and I'm trying to do it with Axios.
Here is the first request, which is in componentWillMount()
axios.get('https://maps.googleapis.com/...
85
votes
2
answers
150k
views
how to get onUploadProgress in axios?
I'm little bit confused that how to upload progress event with axios. Actually I am storing huge number files into aws s3. For that, how to get uploaded progress? I need this function onUploadProgress
...
85
votes
5
answers
260k
views
Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?
I'm trying to authenticate express API back-end using Axios HTTP request call.
I was able to see 'Set-Cookie' in the response header, but cookie was not set. Is it possible to set cookies through ...