httpResponse Object

The httpResponse Object defines the details of the response for the httpRequest.

Properties for the httpResponse Object

body

This is the stream used to read the content body. This is a read-only ReadableStream.
export function responseProvider (request) {
 return httpRequest(`https://${request.host}${request.url}`).then(response => {
   return createResponse(

ok

This is true for 2XX response headers. This is a read-only boolean value.
const response = await httpRequest(subrequestURL);
      if (response.ok) {
		...
        } else {
        ...
      }

status

The HTTP status code. This is a read-only integer.

// HTTP/1.1 200
  response.status;
// => 200

Methods for the httpResponse Object

The following methods are available for the EdgeWorkers httpResponse objects.

getHeader(name)

Provides header values by header name using the same signature as the getHeader() function on callback request objects.

This function returns an array of strings with the values of the header or a value of undefined.
response.getHeader('Content-Type')

getHeaders()

Returns an object with its own properties corresponding to the response headers.

The key is the header name normalized to lowercase and the value is an array of strings containing one string for each HTTP header with the same name. The object is mutable, but independent of the response object - meaning changes won't be reflected by response.getHeader().

getHeaders(s) is only supported where httpRequest() is allowed.
import { httpRequest } from 'http-request';
export async function onClientRequest(request) {
    let response = await httpRequest('/example');
    request.respondWith(response.status, response.getHeaders(), "OK" );

json()

Returns a Promise that resolves to the parsed JSON response.

The response size is limited to 128 KB.
let jsonobject = await response.json()
Note: The response is buffered not streamed.

text()

Reads the body to completion and returns a Promise that resolves to a String containing the full body.

The response size is limited to 128 KB.
let bodytext = await response.text()
Note: The response is buffered not streamed.