Skip to content

How do I get POST data from http request? #9353

Answered by satyarohith
suchislife801 asked this question in Q&A
Discussion options

You must be logged in to vote

You can access the data using .body property of ServerRequest (docs).

Here's a small example:

import { serve } from "https://deno.land/std@0.85.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
const decoder = new TextDecoder();
for await (const req of s) {
  if (req.method === "POST") {
    const buf = await Deno.readAll(req.body);
    const json = JSON.parse(decoder.decode(buf));
    console.log(json);
  }
  req.respond({ body: "Hello World\n" });
}

Run the above program and post some data to the endpoint to see the posted data logged to the console.

curl -x POST -d '{"name": "Deno"}' http://localhost:8000

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@brandonros
Comment options

@kitsonk
Comment options

Answer selected by suchislife801
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants