Today I have learned Put and Post API in javascript
Put and Post API in javascript:
The POST method
The POST method sends data to the server and creates a new resource. The resource it creates is subordinate to some other parent resource. When a new resource is POSTed to the parent, the API service will automatically associate the new resource by assigning it an ID (new resource URI). In short, this method is used to create a new data entry.
postcall = async () => {
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.state.accesstoken}`,
};
const Checkdata = {
email:this.state.email,
password:this.state.password,
};
const res = await axios
.post(`${ip}/post`, Checkdata, {
headers: headers,
})
.then((res) => {
if (res.status === 401) {
alert(" Eligibility Successfully Checked");
}
})
.catch((err) => {
if(err.res){
if(err.res.status===401){
alert("401 error")
}
}
});
};
Output:
The PUT method
The PUT method is most often used to update an existing resource. If you want to update a specific resource (which comes with a specific URI), you can call the PUT method to that resource URI with the request body containing the complete new version of the resource you are trying to update.
putcall = async () => {
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${this.state.accesstoken}`,
};
const Checkdata = {
username:this.state.username,
gender:this.state.gender,
};
const res = await axios
.put(`${ip}/put`, Checkdata, {
headers: headers,
})
.then((res) => {
if (res.status === 401) {
alert(" Eligibility Successfully Checked");
}
})
.catch((err) => {
if(err.res){
if(err.res.status===401){
alert("401 error")
}
}
});
};
Output:

.png)
Comments
Post a Comment