Home > Blockchain >  axios.put is not working when sending query object
axios.put is not working when sending query object

Time:02-15

I was trying to send put request using axios to update information about a book in mongodb, the code is working fine when using postman but it is not working using axios.put used inside react component when submitting, while axios.delete is working fine. I think the problem is that sending the query object in that way is not the right way but I am not able to find the solution. This is the function that handleSubmit,

  • the 'id' is the id of the book

  • 'updates' is a state object that contains all the changes that should happen in the book data.

  • And the second function handleChange is the function that setupdates according to changes in the inputs

    const handleSubmit = async (e) => {
      e.preventDefault();
      try {
        const res = await axios.put("http://localhost:8000/book/edit/"   id, {
          params: {
            updates,
          },
        });
      setNewBook(res.data)
      } catch (err) {
        console.log(err);
      }
    };
    
       const handleChange = (e) => {
          e.preventDefault();
          const value = e.target.value;
          setUpdates({
            ...updates,
            [e.target.name]: value,
          });
        };
    

CodePudding user response:

maybe you misunderstood the axios put method. as per the axios docs provided (https://github.com/axios/axios#axiosputurl-data-config) the correct structure is axios.put(url[, data[, config]])

  • The first parameter is URL
  • The second parameter is data (body request).
  • 3rd parameter is config (you leave params in this parameter)

It should be like this

const res = await axios.put("http://localhost:8000/book/edit/"   id, null, {
       params: {
         updates,
       },
     });

I hope this can help you

  • Related