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
65
votes
2 answers

Using Axios GET with Authorization Header in React-Native App

I'm trying to use axios for a GET request with an API which requires an Authorization header. My current code: const AuthStr = 'Bearer ' + USER_TOKEN; where USER_TOKEN is the access token needed. This string concatenation may be the issue as if I…
Ally Haire
  • 2,398
  • 1
  • 14
  • 19
64
votes
1 answer

Returning an Axios Promise from function

Can someone please explain why returning an Axios promise allows for further chaining, but returning after applying a then()/catch() method does not? Example: const url = 'https://58f58f38c9deb71200ceece2.mockapi.io/Mapss' function…
Matt Stone
  • 3,705
  • 4
  • 23
  • 40
64
votes
6 answers

Force download GET request using axios

I'm using vuejs 2 + axios. I need to send a get request, pass some params to server, and get a PDF as a response. Server uses Laravel. So axios.get(`order-results/${id}/export-pdf`, { params: { ... }}) makes successful request but it does not start…
Victor
  • 5,073
  • 15
  • 68
  • 120
62
votes
10 answers

TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientRequest'

I am a nest.js beginner and I am trying to implement Axios with my code and this error occurs and I would like to fix it. --> starting at object with constructor 'ClientRequest' | property 'socket' -> object with constructor 'Socket' …
ChuChuwi
  • 719
  • 1
  • 8
  • 13
62
votes
11 answers

Vue Axios CORS policy: No 'Access-Control-Allow-Origin'

I build an app use vue and codeigniter, but I have a problem when I try to get api, I got this error on console Access to XMLHttpRequest at 'http://localhost:8888/project/login' from origin 'http://localhost:8080' has been blocked by CORS policy:…
Ashtav
  • 2,586
  • 7
  • 28
  • 44
59
votes
3 answers

Is there any reasons to use axios instead ES6 fetch

Studied the documentation of axios and of ES6 fetch I found than both are quite similar and have experienced a strong influence of $.ajax and its shorthands. Main advantage of axios is browser support. So am I right that if I use babel-polyfill -…
Sasha Kos
  • 2,480
  • 2
  • 22
  • 37
58
votes
4 answers

sending file and json in POST multipart/form-data request with axios

I am trying to send a file and some json in the same multipart POST request to my REST endpoint. The request is made directly from javascript using axios library as shown in the method below. doAjaxPost() { var formData = new FormData(); var…
pavlee
  • 828
  • 1
  • 12
  • 19
58
votes
9 answers

CSRF with Django, React+Redux using Axios

This is an educational project, not for production. I wasn't intending to have user logins as part of this. Can I make POST calls to Django with a CSRF token without having user logins? Can I do this without using jQuery? I'm out of my depth here,…
Reed Dunkle
  • 3,408
  • 1
  • 18
  • 29
55
votes
1 answer

What is Axios default timeout

I found in the documentation steps to set the timeout value. const instance = axios.create({ baseURL: 'https://some-domain.com/api/', timeout: 1000, headers: {'X-Custom-Header': 'foobar'} }); But I could not find the default value in the…
Arr Raj
  • 615
  • 1
  • 6
  • 8
54
votes
6 answers

Axios posting params not read by $_POST

So I have this code: axios({ method: 'post', url, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: { json, type, } }) Originally I had the normal axios.post but I changed to this…
A. L
  • 11,695
  • 23
  • 85
  • 163
53
votes
6 answers

Using JavaScript Axios/Fetch. Can you disable browser cache?

I am trying to query a quote API for a freeCodeCamp project I'm updating to React.js. I am now trying to use Fetch or Axios to query the API but it's caching the response in the browser. I know in $ajax there is a { cache: false } that would force…
Asjas
  • 1,362
  • 1
  • 11
  • 16
52
votes
8 answers

Type of Axios mock using jest typescript

I have the following method in a class: import axios from 'axios' public async getData() { const resp = await axios.get(Endpoints.DATA.URL) return resp.data } Then I am trying to set up a Jest test that does…
Stefan Zhelyazkov
  • 2,599
  • 4
  • 16
  • 41
52
votes
3 answers

How to manage axios errors globally or from one point

I have the standard then/catch axios code all over my app, a simple one goes likes this.. axios.get('/').then( r => {} ).catch( e => {} ) The problem I have with the above is that I have to duplicate the catch() block to handle any potential errors…
hidar
  • 5,449
  • 15
  • 46
  • 70
52
votes
8 answers

Put request with simple string as request body

When I execute the following code from my browser the server gives me 400 and complains that the request body is missing. Anybody got a clue about how I can pass a simple string and have it send as the request body? let content = 'Hello world' …
user672009
  • 4,379
  • 8
  • 44
  • 77
49
votes
4 answers

Download binary file with Axios

For example, downloading of PDF file: axios.get('/file.pdf', { responseType: 'arraybuffer', headers: { 'Accept': 'application/pdf' } }).then(response => { const blob = new Blob([response.data], { type:…
Anton Pelykh
  • 2,274
  • 1
  • 18
  • 21