简体   繁体   中英

Axios PUT Data with Params

My backend API route is /api/updateUser/:id How am I supposed to POST data into this API? I'm familiar with POST request for non params APIs but this one has an /:id in the route.

Can someone show me an example with this demo code

state = {
    username: "random123",
    password: "random123",
    userid: "qwertyuiop",
  };


saveDetails = async () => {
    const { username, password, userid } = this.state;
    let data = new FormData();
      data.append('username',username);
      data.append('password',password);
    axios
      .put(apiEndPoint+'?id='+this.state.userid, data) //this is where I need help
      .then(async (response) => {
        if (response.data) {
          console.log("success");
        } else {
          console.log("issue");
        }
      })
      .catch((err) => {
        console.log("error",err);
      });
  };

This is the working example for Path Parameter Axios PUT request -

saveDetails = async () => {
    const { username, password, userid } = this.state;
    axios
      .put(apiEndPoint+"updateUser/"+userid, {
        username:username,
        password:password,
      })
      .then(async (response) => {
        if (response.data) {
          console.log("done");
          
        } else {
          console.log("error");
        }
      })
      .catch((err) => {
        console.log("error",err);
      });
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM