Async iterable iterator of records.
If you want array of records, use parseRequest.toArray function.
This function automatically treats request headers.
Content-Type header is not set, it assumes text/csv.Content-Type header is not text/csv, it throws an error.Content-Type header has charset parameter, it uses it for decoding.Content-Encoding header is set, it decompresses the request.This function is particularly useful for server-side environments like Cloudflare Workers, Service Workers, or other edge computing platforms that use the Request API.
import { parseRequest } from 'web-csv-toolbox';
export default {
async fetch(request: Request) {
if (request.method === 'POST' && request.headers.get('content-type')?.includes('text/csv')) {
for await (const record of parseRequest(request)) {
console.log(record);
}
return new Response('CSV processed', { status: 200 });
}
return new Response('Not Found', { status: 404 });
}
};
import { parseRequest } from 'web-csv-toolbox';
self.addEventListener('fetch', (event) => {
const request = event.request;
if (request.method === 'POST' && request.url.endsWith('/upload-csv')) {
event.respondWith(
(async () => {
const records = [];
for await (const record of parseRequest(request)) {
records.push(record);
}
return new Response(JSON.stringify(records), {
headers: { 'Content-Type': 'application/json' }
});
})()
);
}
});
Parse HTTP Request what contains CSV to records, ideal for server-side use cases.