This is referred from: https://medium.com/deno-the-complete-reference/sending-form-data-using-fetch-in-node-js-8cedd0b2af85
All credit to: https://choubey.medium.com/
This is referred from: https://medium.com/deno-the-complete-reference/sending-form-data-using-fetch-in-node-js-8cedd0b2af85
All credit to: https://choubey.medium.com/
| 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()); |