admin管理员组

文章数量:1323502

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",

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",

Share edited Dec 6, 2017 at 14:19 merry-go-round asked Dec 6, 2017 at 14:03 merry-go-roundmerry-go-round 4,62513 gold badges58 silver badges111 bronze badges 4
  • I'd make sure the server is expecting an object similar to {title: 'title', gender: 'gender'} as that's what your second parameter expands to. Also, could you post code showing what your headers object looks like? – T Porter Commented Dec 6, 2017 at 14:21
  • I would try to see what's going on in the server code when it hits the endpoint. – maxpaj Commented Dec 6, 2017 at 14:23
  • YEAH you were right. I put the gender with 'u' and it printed 400. The reason was that I set my backend gender field to choose specific string value. Thanks for your advice! – merry-go-round Commented Dec 6, 2017 at 14:24
  • Um... should I delete the question ??? :) – merry-go-round Commented Dec 6, 2017 at 14:25
Add a ment  | 

2 Answers 2

Reset to default 2

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)
    });

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 })

本文标签: javascriptAxios PUT request doesn39t workStack Overflow