new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Cloudflare Workers · all subjects

request-response

131 notes in this subject, read out of this brain and free to use. This is page 3 of 3.

Request method validation in fetch handler

Use `request.method` to check the HTTP method of incoming requests. For example, check if a request is a POST request by comparing `request.method !== 'POST'` and return an error response if it does not match the expected method.

Parse webhook JSON payload from request body

To access the webhook payload data, first call `await request.text()` to get the raw body as a string, then parse it with `JSON.parse()` to access the webhook data. The parsed object contains properties like `repository.full_name` for the repository name and `sender.login` for the user who triggered the webhook.

HTTP 405 Method Not Allowed response

Return a Response with status code 405 when a request uses an HTTP method that is not allowed for the given endpoint. This indicates the resource exists but the client used an unsupported method like GET when only POST is accepted.

Parse form data in Workers using formData()

To extract form data from a request body, call await request.formData() to get a FormData object, then convert it to a plain object using Object.fromEntries(body) to access form fields by name. The parsed fields can then be destructured or accessed individually.

Workers fetch handler basic structure

A Workers fetch handler exports a default object with an async fetch method that receives (request, env) parameters. The request object contains information about the incoming HTTP request including url, method, and body. The env object provides access to bindings, secrets, and vars configured in wrangler.toml.

Form submission handler routing in Workers

A Worker fetch handler routes incoming requests by checking the URL pathname. If the pathname is '/submit', it calls a submit handler; otherwise it returns a 404 Not Found response. The handler structure checks request.method to ensure only POST requests are accepted to the submit endpoint, returning a 405 Method Not Allowed status for other HTTP methods.

Worker fetch handler structure for OpenAI

The Worker export default object must have an async fetch(request, env, ctx) method. Within this method, instantiate the OpenAI client using env for accessing secrets, extract parameters from the request URL using new URL(request.url), build the messages and tools arrays, call the OpenAI API, and return responses as new Response objects.

Access secrets in Worker via env parameter

Secrets added to a Worker are accessed through the env parameter passed to the Worker's fetch event handler. For example, env.POSTMARK_API_TOKEN retrieves the Postmark API token stored as a secret.

Cloudflare-Workers-Version-Overrides header

Use the `Cloudflare-Workers-Version-Overrides` header to send a request to a specific version of your Worker in the current deployment, even those set to serve 0% of traffic. This header is a Dictionary Structured Header (RFC 8941) that can contain multiple key-value pairs. Each key indicates the name of the Worker the override should be applied to, and the value indicates the version ID that should be used as a String.

Version override request example

Example of using version overrides in a curl request: curl -s https://example.com -H 'Cloudflare-Workers-Version-Overrides: my-worker-name="dc8dcd28-271b-4367-9840-6c244f84cb40"'

Top-level navigation requests with Sec-Fetch-Mode header

For top-level navigation requests, browsers send a Sec-Fetch-Mode: navigate header. If this header is present and the URL does not match a static asset, the not_found_handling behavior is invoked rather than the Worker by default. This is implicit routing based on the header.

Give your agent this brain