Skip to content

Instantly share code, notes, and snippets.

@RishikeshDarandale
Last active September 28, 2024 16:40
Show Gist options
  • Save RishikeshDarandale/8f424c2d79884857a3ce1d12052483d3 to your computer and use it in GitHub Desktop.
Save RishikeshDarandale/8f424c2d79884857a3ce1d12052483d3 to your computer and use it in GitHub Desktop.
Print the data is received by the server API & posting a file to API from node.js environment
import { readFile } from "node:fs/promises";
const fileName = "./sample.txt";
const body = new FormData();
const blob = new Blob([await readFile(fileName)]);
# if you have base64 string
# const blob = new Blob([Buffer.from(base64Data, 'base64')]);
body.set("field1", "val1");
body.set("field2", "val2");
body.set("file1", blob, fileName);
const resp = await fetch("https://echo-server.deno.dev", {
method: "POST",
body,
});
console.log(
"STATUS:",
resp.status,
"\nCONTENT TYPE:",
resp.headers.get("content-type"),
);
console.log("RAW BODY:", await resp.text());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment