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.

Promise based HTTP client for the browser and Node.js, available on Github.

Features

  • Make XMLHttpRequest from the browser
  • Make HTTP requests from Node.js
  • Supports the Promise API
  • Intercept request and response
  • Transform request and response data
  • Automatic transforms for JSON data
  • Client side support for protecting against XSRF

Installation

Using npm:

npm install axios

Using bower:

bower install axios

Using yarn:

yarn add axios

Using jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

Using unpkg CDN:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
24271 questions
552
votes
16 answers

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); //Logs a string: Error: Request failed with status code…
Sebastian Olsen
  • 10,318
  • 9
  • 46
  • 91
439
votes
16 answers

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 correct? Or should I do: axios.post(url, params:…
user2950593
  • 9,233
  • 15
  • 67
  • 131
438
votes
19 answers

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 POST Request: var body = { userName: 'Fred', …
Srikanth Gowda
  • 6,163
  • 7
  • 19
  • 34
332
votes
12 answers

What is difference between Axios and Fetch?

I am calling the web service by using Fetch but the same I can do with the help of Axios. So now I am confused. Should I go for either Axios or Fetch?
Gorakh Nath
  • 9,140
  • 15
  • 46
  • 68
323
votes
14 answers

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 this example, the response to the request will be…
mignz
  • 3,648
  • 2
  • 20
  • 21
316
votes
16 answers

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 header and I can see that all the fields that I…
TWONEKSONE
  • 3,918
  • 3
  • 19
  • 26
312
votes
10 answers

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((response) => { dispatch({type: FOUND_USER,…
Jagrati
  • 11,474
  • 9
  • 35
  • 56
309
votes
18 answers

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: { 'Authorization': 'Bearer ' + validToken() …
rakibtg
  • 5,521
  • 11
  • 50
  • 73
305
votes
19 answers

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 by hand. This is my clientside request…
Kunok
  • 8,089
  • 8
  • 48
  • 89
302
votes
9 answers

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 manually attach it to every request in the…
awwester
  • 9,623
  • 13
  • 45
  • 72
300
votes
9 answers

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:
Don Smythe
  • 9,234
  • 14
  • 62
  • 105
275
votes
20 answers

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? If not, what else can I use in a React…
David Choi
  • 6,131
  • 10
  • 28
  • 28
273
votes
19 answers

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 payload = { "username": .. } I've been searching…
Asfourhundred
  • 3,003
  • 2
  • 13
  • 18
261
votes
10 answers

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'; var credentials =…
Emmanuel
  • 2,957
  • 3
  • 14
  • 17
237
votes
4 answers

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 (dispatch) { …
Sinan Samet
  • 6,432
  • 12
  • 50
  • 93
1
2 3
99 100