Skip to content

Commit

Permalink
Add a dedicated example for the File.readAll method
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Aug 29, 2023
1 parent 6c93997 commit beeb096
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/experimental/fs/readAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import http from "k6/http";
import { open } from "k6/experimental/fs";

export const options = {
vus: 1,
iterations: 1,
};

// As k6 does not support asynchronous code in the init context, yet, we need to
// use a top-level async function to be able to use the `await` keyword.
let file;
(async function () {
file = await open("bonjour.txt");
})();

export default async function () {
// Obtain information about the file.
const fileinfo = await file.stat();
if (fileinfo.name != "bonjour.txt") {
throw new Error("Unexpected file name");
}

let res = http.post(
"https://httpbin.test.k6.io/post",
await file.readAll(),
{
headers: { "Content-Type": "text/plain" },
}
);

console.log(res.body);
}

0 comments on commit beeb096

Please sign in to comment.