How to get data from Cloudflare worker GET/POST requests

Cloudflare worker

Cloudflare Workers provides a serverless execution environment that allows you to create entirely new applications or augment existing ones without configuring or maintaining infrastructure.

Documentation for Cloudflare worker.

Cloudflare-worker-request-data

Getting data from Cloudflare-worker requests is not as easy as you were working with Express.js on Nodejs, it is necessary to understand the organization of request in Cloudflare-worker.

Cloudflare-worker-request-data is a JavaScript package released under MIT license, which can help you with getting data from a GET or POST request. With this package, it is not necessary to study the API of request from documentation file, and the usage is quite simple and straightforward.

Installation

1
npm i cloudflare-worker-request-data

Example

1
2
3
4
5
6
7
8
9
10
11
import { RequestData } from 'cloudflare-worker-request-data';

export default {
async fetch(request, env, ctx) {

const data = await RequestData(request)

// do something with the data

}
};

Description

RequestData(request) in the above example can get return of

  • a String in case of “application/text” or “text/html”
  • a Object in case of “application/json”, “application/x-www-form-urlencoded”, “mutipart/form-data”
  • a String which is empty if nothing parsed
  • The request shall be either in GET or POST, other methods of request will directly return you with an empty String.