merry-go-round
merry-go-round

Reputation: 4615

Axios PUT request doesn't work

axios.put method doesn't work while axios.post works.

This is working example for post request. (example)

let response = await axios.post(`${ROOT_URL}/urls/url/`, {
    post_id,
    password,
    content
  }, { headers });
  console.log(response.status) // 201.

I just copied and pasted the valid post request, and modified some fields and method for put request. But it returns 400 error on server side.

let response = await axios.put(`${ROOT_URL}/profile/update/`, {
    title,
    gender
  }, { headers }); <- Server prints 400 HTTP error.

I tested with Postman and I confirmed that it works with put method. I have to think that my syntax for axios.put is wrong but I'm not sure how it can be different with the post method.

If you see axios's official doc page, it looks almost identical. Axios documentation link

And axios version is 0.16.2 : "axios": "^0.16.2",

Upvotes: 0

Views: 19974

Answers (2)

Alireza tk
Alireza tk

Reputation: 475

I post this here maybe it answers someone's problem

if in your api setup in the backend. the route is configured like this 'api/user1 or some other mode of identification/someData/anotherData'

you should send the data exactly as such and not as an object. so in my case it was like this: axios.put(`${Routes.ConfrimCode}${phoneNum}/${imei}/${code}`).then(res=>{ //callback })

Upvotes: 1

Vince Lowe
Vince Lowe

Reputation: 3620

400 is bad request not 405 method not allowed.

I'd check what your posting is correct.

Is this a valid object?

{
    title,
    gender
}

Example:

axios.put('/api/something', {
    foo: bar
})
    .then(function (response) {
        // do something...
    }.bind(this))
    .catch(function (error) {
        console.log(error)
    });

Upvotes: 2

Related Questions