question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Behaviour of Axios causing 400

See original GitHub issue

Summary

I am having an error with a Yii2 based application where a fetch call successfully submits but switching out for axios returns a 400. I have been unsuccessful in fixing this issue. My current code is as follows, where form is the DOM element.

 var args = { method: 'POST', body: new FormData(form), data:{}, config: { headers: {'Content-Type': 'multipart/form-data' } } }

  // Submit form and update previousState
  fetch("/", args).then(()=>setContent(form));
  // axios("/", args).then(()=>setContent(form));

I have tried playing around with the args and such, but didn’t fix it.

The result from the server when fetch is used is 200 OK and when axios is used it returns 400 Bad Request The follow stack traces

Error: Request failed with status code 400
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:59)
dispatchXhrRequest | @ | xhr.js:172
-- | -- | --
  | xhrAdapter | @ | xhr.js:11
  | dispatchRequest | @ | dispatchRequest.js:59
  | Promise.then (async) |   |  
  | request | @ | Axios.js:53
  | wrap | @ | bind.js:9
  | _callee2$ | @ | autosave.js:162
  | tryCatch | @ | runtime.js:45
  | invoke | @ | runtime.js:271
  | prototype.<computed> | @ | runtime.js:97
  | asyncGeneratorStep | @ | asyncToGenerator.js:3
  | _next | @ | asyncToGenerator.js:25
  | Promise.then (async) |   |  
  | asyncGeneratorStep | @ | asyncToGenerator.js:13
  | _next | @ | asyncToGenerator.js:25
  | eval | @ | asyncToGenerator.js:32
  | eval | @ | asyncToGenerator.js:21
  | submit | @ | autosave.js:91

(autosave.js is my file)

Context

axios@^0.19.0: version “0.19.0” node v10.16.0 Mac OS 10.14.6 running mamp PRO 5.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
smilledgecommented, Oct 21, 2019

Axios and fetch have different apis, and the same set of args wont be compatible between them.

Eg, this fetch request;

fetch('/', {
  method: 'POST', 
  body: new FormData(form),
  headers: {
    'Content-Type': 'multipart/form-data' 
  }
})

Would look like this in Axios;

axois.post('/', new FormData(form), {
  headers: {
    'Content-Type': 'multipart/form-data' 
  }
})

If you’re still having issues check the network tab to see if the request body is being sent correctly. Also, StackOverflow is more suitable for questions like this as it’s not an issue with Axios.

0reactions
aman79commented, Feb 20, 2020

getting error for get request

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to catch the body of an AXIOS error - DEV Community ‍ ‍
In many instances the status of our request may return some kind of error (400, 500), and when this happens we would like...
Read more >
Getting 400 error Bad request using axios - Stack Overflow
I am using react-redux and trying to send a post request to localhost:3000/posts. Here is the code that I am using. import axios...
Read more >
axios bad request 400 | The AI Search Engine You Control
I keep getting a 400 bad request from Axios. I can pass a GET request and confirm its working fine. I create http-common.js...
Read more >
Handling Errors | Axios Docs
axios.get('/user/12345') .catch(function (error) { if (error.response) { // The request was made and the server responded with a status code // that falls ......
Read more >
[Solved]-Axios 400 error request call then instead of catch-Vue.js
This is on purpose in older version of Axios. validateStatus has been added into config since v0.11. We can use this option to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found