got 405 error while updating with put method
Unanswered
Northeast Congo Lion posted this in #help-forum
Northeast Congo LionOP
I try to update data with put method but gives error
error:
route.ts file of advertisement
server action file
405. error:
AxiosError {message: 'Request failed with status code 405', ....
..........................const handleUpdateAd = async () => {
try {
const res = await axios.put(`/api/advertisement?adId=${adId}`, {
title: adTitle,
link: adLink,
image: imagePreview,
});
console.log("res of handleUpdateAd:", res);
} catch (error) {
console.log("error in updating ad:", error);
}
};route.ts file of advertisement
export async function POST(req: Request) {
const body = await req.json();
const url = new URL(req.url);
const adId = url.searchParams.get("adId");
if(adId){
try{
body.adId = adId;
const ad = await updateAd(body);
return NextResponse.json({message:'Advertisement Updated Successfully', advertisement: ad})
}catch(error){
console.log('error in UpdateAd post:', error);
}
}else{
try {
console.log("body:", body);
const advertisement = await createAdvertisement(body);
return NextResponse.json({
message: "Advertisement created successfully",
advertisement,
});
} catch (error) {
console.log("Error in Post request while creating Advertisment:", error);
// return NextResponse.error({message: 'Internal server error', error, status: 500})
}
}
}server action file
export async function updateAd(adData: any){
try{
const {adId, ...rest} = adData;
const ad = await Advertisement.findByIdAndUpdate({_id: adId}, rest, {new: true});
if(!ad){
throw new Error('Advertisement is not updated');
}
return JSON.parse(JSON.stringify(ad));
}catch(error){
console.log('Error while updating Ads:', error);
handleError(error);
}
}10 Replies
@joulev It should be export async function **PUT** instead of POST
Northeast Congo LionOP
any solution, for I use post for two function as you can see
@Northeast Congo Lion any solution, for I use post for two function as you can see
What do you mean? You very clearly used the put method on the frontend, while your route only handles post method, that’s the bug
@joulev What do you mean? You very clearly used the put method on the frontend, while your route only handles post method, that’s the bug
Northeast Congo LionOP
👍 I got that
I said, I use that post method in route for 2 endpoints, and add differentiate endpoints on id.
should I go for nested routing for edit and delete
should I go for nested routing for edit and delete
instead of passing 2 endpoints in 1 POST METHOD
I don’t understand your question so sorry I can’t help. You will need someone else to help you
Northeast Congo LionOP
wait bro
Schneider’s Smooth-fronted Caiman
Hm
Northeast Congo LionOP
this is endpoint I use before:
api
advertisement
route.ts
//////////////////////////////////
after or now I try this
api
advertisement
route.ts
editAd
route.ts
in 2nd option, I create 1 more route editAd, for update or put method, is it good practice.
const res = await axios.put(`/api/advertisement?adId=${adId}`)api
advertisement
route.ts
//////////////////////////////////
after or now I try this
const res = await axios.put(`/api/advertisement/editAd?adId=${adId}`)api
advertisement
route.ts
editAd
route.ts
in 2nd option, I create 1 more route editAd, for update or put method, is it good practice.