Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make a PUT request with axios?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading