Request

The Request Web API allows you to construct a request object to be used in fetch, but also represents the inbound request for your Repeat webhook and gives you access to the request headers, body and other properties.

Signature

declare class Request extends Body {
  constructor(input: Request | string, init?: RequestInit | Request);
  clone(): Request;
  readonly method: string;
  readonly url: string;
  readonly headers: Headers;
  readonly redirect: string;
  readonly fetcher: Fetcher | null;
  readonly signal: AbortSignal;
  readonly cf?: IncomingRequestCfProperties;
}

Constructor

  • Request(): Request
    • The Request() constructor creates a new Request object.

Properties

  • body: ReadableStream
    • A ReadableStream representing the request’s body. This can be accessed via helper methods such as text() and json().

  • bodyUsed: boolean
    • A boolean used to tell if the body has been consumed or not.

      Example Usage
      Example: bodyUsed
      export default {
      	async webhook(req: Request, env: Repeat.Env) {
      		console.log(req.bodyUsed); // false
      		const body = await req.text();
      		console.log(req.bodyUsed); // true
      		
      		const body2 = await req.text(); // Exception: body has already been used.
      	}
      }
  • method: string
    • Represents the method used by the request, such as GET or PUT.

  • url: string
    • Represents the URL used by the request. This is not a URL object but just a string so you can use new URL(request.url) to convert it.

  • headers: Headers
    • A Headers object used by the request. Headers cannot be accessed via indexing, such as headers["content-type"], and instead expose methods such as get() or set().

  • redirect: string
    • The redirect mode used by the request. This tells the request when used with fetch if it should follow redirects, return the redirect response to the caller or throw an error. Accepted values: follow, error, manual Default value: follow when constructed by new Request(), otherwise manual.

  • signal: AbortSignal
    • The AbortSignal used by the request. This can be used to set a timeout for your outbound requests.

      Example Usage
      Using an AbortSignal to timeout your fetch requests after 5 seconds
      await fetch("https://example.com", {
        signal: AbortSignal.timeout(5000) // 5 seconds
      });

Methods

  • clone() → Request
    • Creates a cloned instance of the request.

  • arrayBuffer() → Promise<ArrayBuffer>
  • text() → Promise<string>
    • Access the request’s body as plain-text.

  • json<T>() → Promise<T>
    • Access the request’s body as JSON. This will throw an error if the body is not well-formed JSON, and we recommend you check that the content-type header is application/json.

  • formData() → Promise<FormData>
    • Access the request’s body as FormData. This will throw an error if the body is not sent as FormData, and we recommend you check if the content-type header contains form.

  • blob() → Promise<Blob>
    • Access the request’s body as a Blob.

Examples

Access properties on the inbound request

Example #1
export default {
	async webhook(request: Request, env: Repeat.Env) {
		// ...
		console.log('incoming request', `${request.method} ${request.url}`);
		// ...
	},
};

Create a new Request & fetch it

Example #2
export default {
	async webhook(request: Request, env: Repeat.Env) {
		const req = new Request('https://httpbin.org/anything', {
			headers: {
				'x-foo': 'bar',
			},
		});

		const resp = await fetch(req);

		console.log(resp.status);
		// 200
	},
};

How to create a POST request with a body

Example #3
export default {
	async webhook(request: Request, env: Repeat.Env) {
		const req = new Request('https://httpbin.org/anything', {
			headers: {
				'x-foo': 'bar',
			},
			method: 'POST',
			body: '123',
		});

		const resp = await fetch(req);

		console.log(await resp.text());
		// ...
		// "json": 123,
		// "method": "POST",
		// ...
	},
};
 

Last updated on November 20, 2022