Ask coding questions

← Back to all posts
Receive Fetch Data
ruiwenge2 (582)

I'm using fetch in JavaScript to send some data to the server(Flask). So I did:

fetch("save", {
  method:"POST",
  body:JSON.stringify({
    contents:html
  })
}).then(console.log("saved"));

But how do I receive and find the value of contents:html in Flask?

Answered by Coder100 (18123) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18123)

you can console.log the response, like this:

fetch("save", {
  method:"POST",
  body:JSON.stringify({
    contents:html
  })
}).then(r => r.text()).then(console.log);
ruiwenge2 (582)

@Coder100 i meant how do i get the value of html in Flask?

Coder100 (18123)

@ruiwenge2 don't forget to also add the json header

fetch("save", {
  method:"POST",
  headers: { "Content-Type": "application/json" }
  body:JSON.stringify({
    contents: html
  })
})

https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask

HoVo2 (0)

f53e1b5e-0150-4403-a932-5cabfeea1136

ruiwenge2 (582)

btw the save url is actually the url to receive data