Inke
Inke•4d ago

how do I access post data in a deno http server?

Hi y'all, I am perfectly aware that it may be my limited knowledge on the JS eco system, but I have not found a warking way to access POSTed data in a deno http Server. What do I have to do here? // example Server async function handler(req: Request): Promise<Response> { const url = new URL(req.url); const path = url.pathname; const method = req.method; let module; console.log( req) if ("GET" === method) { return new Response("Hello", { status: 200 }) } if ("POST" === method) { // What do I have to do here, to get the data? // I've tried: readAll // const buf = await req.body(); // const buf = await readAll(req.body); // const json = JSON.parse(decoder.decode(buf)); // const decoder = new TextDecoder(); // decoder.decode( req.body) // const body = await req.formData() return new Response("Hello, where's my data?", { status: 200 }) } return new Response("Method not implemented", { status: 501 }); } Deno.serve(handler); // Following is an example curl call: curl -d "param1=value1&param2=value2" -X POST http://localhost:8000/test Thank you for your time and help. Inke
7 Replies
veodium
veodium•4d ago
if ("POST" === method) { const body = JSON.parse(await req.text()); console.log(body); return new Response("Hello, where's my data?", { status: 200 }) } try this
raunioroo
raunioroo•4d ago
For URL encoded post data (like your curl call seems to be), I use this:
const postdata = await request.text();
// console.log("POSTDATA", postdata);
const searchParams = new URLSearchParams(postdata);
const post = {};
for (const [key, value] of searchParams.entries()) {
// console.log("FOUND", key, value)
post[key] = value;
}
const postdata = await request.text();
// console.log("POSTDATA", postdata);
const searchParams = new URLSearchParams(postdata);
const post = {};
for (const [key, value] of searchParams.entries()) {
// console.log("FOUND", key, value)
post[key] = value;
}
For raw json:
const postdata = await request.text();
return JSON.parse(postdata);
const postdata = await request.text();
return JSON.parse(postdata);
For multipart post:
const formData = await request.formData();
const entries = {};
for (const [key, value] of formData) {
entries[key] = value;
}
return entries;
const formData = await request.formData();
const entries = {};
for (const [key, value] of formData) {
entries[key] = value;
}
return entries;
...and for detecting which one to use:
const contentType = request.headers.get("content-type");
if (contentType == "application/x-www-form-urlencoded") {
// url encoded
} else if (contentType.includes("multipart/form-data")) {
// multipart
} else if (contentType == "application/json") {
// json
}
const contentType = request.headers.get("content-type");
if (contentType == "application/x-www-form-urlencoded") {
// url encoded
} else if (contentType.includes("multipart/form-data")) {
// multipart
} else if (contentType == "application/json") {
// json
}
Inke
InkeOP•4d ago
Thank you veodium, req.text() works. And thank you ranioroo, this would probably have been my next stumbling block. Why is this so hard to find? (Well guess, it's common knowledge for everyone else 🙂
raunioroo
raunioroo•4d ago
Well, I remember all this was surprisingly hard/painful to figure out, since there are so many different ways to POST, and all needed to be handled in a different way, wasn't that obvious :)
veodium
veodium•4d ago
read examples in the official document and more and more googling 🙂
NDH
NDH•4d ago
if ("POST" === req.method) {
if (req.body) return new Response(JSON.parse(await req.text()),{ status: 200 })
}
if ("POST" === req.method) {
if (req.body) return new Response(JSON.parse(await req.text()),{ status: 200 })
}
Inke
InkeOP•3d ago
I know, google is my friend, but it turned out to be a fickly friend and was giving me pre 2.0 answers which I didn't get to work.

Did you find this page helpful?