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
197
votes
4 answers

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 : …
GuillaumeRZ
  • 2,656
  • 4
  • 19
  • 35
173
votes
3 answers

How to return values from async functions using async-await from function?

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.com/posts'); return…
King Rayhan
  • 2,287
  • 3
  • 19
  • 23
168
votes
7 answers

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; }, function (error) { // Do something with…
Akash Salunkhe
  • 2,698
  • 2
  • 16
  • 31
157
votes
6 answers

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 am able to log a generic console.log. However I…
David Choi
  • 6,131
  • 10
  • 28
  • 28
155
votes
7 answers

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 Axios in this case? Jest has this use case on…
Adear
  • 1,885
  • 2
  • 11
  • 18
149
votes
12 answers

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(URLConstants.USER_URL, { headers: { header } }); Gives me…
foobar
  • 3,849
  • 8
  • 22
  • 32
147
votes
6 answers

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.data however it doesn't seem to work like that.…
Hobbyist
  • 15,888
  • 9
  • 46
  • 98
136
votes
14 answers

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 Postman and I get the correct JSON back. This is…
shawnest
  • 1,389
  • 2
  • 6
  • 10
135
votes
20 answers

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' header is present on the requested resource.…
SeaWarrior404
  • 3,811
  • 14
  • 43
  • 65
134
votes
12 answers

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 to add headers in axios request using various…
Kiran
  • 2,147
  • 6
  • 18
  • 35
132
votes
10 answers

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 actual data after any scroll event. Problem that…
Rajab Shakirov
  • 7,265
  • 7
  • 28
  • 42
129
votes
11 answers

Returning data from Axios API

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 when the script returns the data from the call…
bunkerguy
  • 1,393
  • 2
  • 9
  • 4
120
votes
11 answers

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: Response to preflight request doesn’t pass access…
GGG
  • 1,307
  • 3
  • 7
  • 11
118
votes
9 answers

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 verify the SSL certificate used on the…
Jemi Salo
  • 3,401
  • 3
  • 14
  • 25
117
votes
18 answers

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…
Zin Kun
  • 1,273
  • 2
  • 8
  • 6