Next.js Discord

Discord Forum

Problem In POST Request using fetch

Answered
Kishu posted this in #help-forum
Open in Discord
KishuOP
this is a post request,

let data = { id:"1", title: "hello" }

await fetch(http://127.0.0.1:8000/api/user/, {
method: 'post',
mode:'cors',
headers:{
'Content-Type':'application/json',
'Authorization': 'token reoproweproopo3423432'
},
body: JSON.stringify(data)
});



Now, here the backend api is from django, http://127.0.0.1:8000/api/user/.
When I make a post request, then I am getting 400.
Is it problem front-end or back-end ?
How to fix it ? I think cors is enabled in django as well.
Answered by Anay-208
Back end
View full answer

19 Replies

Answer
KishuOP
@Anay-208 thanks
Mark the above message as a solution please. Thanks
@Anay-208 Back end
@Kishu Mark this message as a solution if you don’t mind
KishuOP
yes sure.
how to mark it ?
i am seeing mark unread
given
@Anay-208 , then if i say backend developer, what shoud I say them to do ?
@Kishu <@755810867878297610> , then if i say backend developer, what shoud I say them to do ?
You mean you need to tell backend developer about this error?
and can you send your console?
KishuOP
yes
@Kishu yes
Just send screenshot of console first
KishuOP
console.log is ...
"use client"

const handleSubmit = async () => {
await fetch(http://127.0.0.1:3000/api/user/, {
method:'post',
mode:'cors'
headers: {
'Content-Type':'appli...'
'Authorization':'Token adasda'
}
})
}

here in this code, the error is coming from await fetch(http://127.0.0.1:3000/api/user/`` .. cannot do this like that this line.
in the django console, it is coming 400 error.
KishuOP
it works now
i was facing problem in next js
i could not understand the difference between server componentn and client componet