DEV Community

WTSolutions
WTSolutions

Posted on

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.

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

npm i cloudflare-worker-request-data
Enter fullscreen mode Exit fullscreen mode

Example

import { RequestData } from 'cloudflare-worker-request-data';

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

        const data = await RequestData(request)

        // do something with the data           

    }
};
Enter fullscreen mode Exit fullscreen mode

Description

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

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

Top comments (0)