How do I get POST data from http request? #9353
Answered
by
satyarohith
suchislife801
asked this question in
Q&A
-
Hello. I can't seem to find this information anywhere. If I make a post request with JSON data, how do I get this posted data from the request?
TS2341 [ERROR]: Property 'buf' is private and only accessible within class 'BufReader' |
Beta Was this translation helpful? Give feedback.
Answered by
satyarohith
Feb 1, 2021
Replies: 1 comment 2 replies
-
You can access the data using 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 |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
suchislife801
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can access the data using
.body
property ofServerRequest
(docs).Here's a small example:
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