Response

The `Response` class represents an HTTP response.

Constructor

new Response(init)

Creates a new instance of the Response class.
Parameters:
NameTypeDescription
initobjectOptional. An initial object to populate the response.
Properties
NameTypeAttributesDefaultDescription
init.statusCodenumber<optional>
200The status code of the response.
init.statusPhrasestring<optional>
"OK"The status phrase of the response.
init.headersobject<optional>
{}The headers of the response.
init.textBodystring<optional>
nullThe text body of the response.
Example
// Creating a new instance of the Response class with custom values
const response = new Response({
  statusCode: 404,
  statusPhrase: 'Not Found',
  headers: {
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache'
  },
  textBody: '{"error": "Resource not found"}'
});
console.log(response.encode());

Members

body

Gets or sets the body of the response.
Properties
NameTypeDescription
getterBodyGets response body as type {Body}.
setterstring | Map | Array | URLSearchParamsSets response body with typeof {string | Map | Array | URLSearchParams}.
Examples
// Getting the body of the response
console.log(response.body); // Output: Body { ... }
// Setting the body of the response using a string
response.body = 'This is the response body as a string';
console.log(response.body.parseText());
// Setting the body of the response using JSON data
const jsonData = { message: 'Response JSON data' };
response.body = JSON.stringify(jsonData);
console.log(response.body.parseText());
// Setting the body of the response using URLSearchParams
const urlSearchParams = new URLSearchParams();
urlSearchParams.append('param1', 'value1');
urlSearchParams.append('param2', 'value2');
response.body = urlSearchParams;
console.log(response.body.parseText());

headers

Gets the headers of the response.
Properties
NameTypeDescription
getterHeadersGets the headers of the response.
Example
// Getting the headers of the response
console.log(response.headers); // Output: Headers { ... }

statusCode

Sets or gets the status code of the response.
Properties
NameTypeDescription
getternumberGets the status code of the response.
setternumberSets the status code of the response.
Examples
// Getting the status code of the response
console.log(response.statusCode); // Output: 40
// Setting the status code of the response
response.statusCode = 500;
console.log(response.statusCode); // Output: 500

statusPhrase

Sets the status phrase of the response.
Properties
NameTypeDescription
getterstringGets the status phrase of the response.
setterstringSets the status phrase of the response.
Examples
// Getting the status phrase of the response
console.log(response.statusPhrase); // Output: Not Found
// Setting the status phrase of the response
response.statusPhrase = 'Internal Server Error';
console.log(response.statusPhrase); // Output: Internal Server Error