Home > OS >  How do I make a PUT request with axios?
How do I make a PUT request with axios?

Time:05-05

I'm trying to update a field on my MongoDB database with Axios PUT method in React JS. I'm also using react form hook.

const onSubmit = async itemData => {
    console.log(itemData);
    const url = `http://localhost:5000/items/${id}`
    const { data } = await axios.put(url, {
        productQTY: itemData.productQTY,
        headers: {
            authorization: `Bearer ${localStorage.getItem('accessKey')}`
        }
    })

so I can access itemData.productQTY from itemData. There is also a field in the Database named productQTY, so how do I update the field?

CodePudding user response:

const onSubmit = async itemData => {
    console.log(itemData);
    const url = `http://localhost:5000/items/${id}`
    const { data } = await axios.put(url,
        { productQTY: itemData.productQTY },
        { 
         headers: 
           {
            authorization: `Bearer ${localStorage.getItem('accessKey')}`
           }
        }
    })

u can try it, with data and header in 2 params of put request.

  • Related