Builds HTTP responses to server requests. The constructor can take the code as a number or an HttpStatusCode value.

const resp1 =  new HttpResponse(404);
const resp2 = new HttpResponse(HttpStatusCode. UNAUTHORIZED); 

You can apply method chaining. All methods return the response object itself after they change it.

const resp =  new HttpResponse();
resp 
    .status(201) 
    .content('awesome') 
    . json({content: 'created'}) 
    .cookie('sessionId', 'uniqueSessId') 
    . location('https://example.com/path') 
    .redirect('/same_url', 301) 
    . set('Content-Type', 'text/html'); 
return resp; 

Hierarchy

  • HttpResponse

Methods

content

  • content(data: string): this
  • Set the format of the response body content to string.

    Parameters

    • data: string

      String storing the data of the response body.

    Returns this

cookie

  • cookie(name: string, value: string, options?: CookieOptions): this
  • Set the value of a cookie.

    Parameters

    • name: string

      Cookie name.

    • value: string

      Cookie values.

    • Optional options: CookieOptions

      Options for passing and storing cookies.

    Returns this

json

  • json(data: any): this
  • Set the response body from a JSON object.

    Note that when transferring a response, the JSON.stringify() function is used, and if any circular references are present, the request will return an error. This method also sets the response’s heading Content-Type: application/json. If needed, you can set a different value.

    return new HttpResponse (HttpStatusCode.CREATED)
        .json({content:  'created'}) 
        .set('Content-Type', 'text/javascript'); // Old IE now  happy 
    

    As data, you can pass an instance object in which the json() function returns the state object of this instance.

    function MyClass() {};
    MyClass.prototype.json = function() { 
      return {  id: 1, value: 'string' } 
    }; 
    const myData = new MyClass(); 
    const resp =  new HttpResponse().json(myData); 
    

    Parameters

    • data: any

      Data in the JSON object, array, or function instance format.

    Returns this

location

  • location(url: string): this
  • Set Location as the HTTP header.

    Parameters

    • url: string

      Link value set in Location.

    Returns this

redirect

  • Redirect request. By default, the 302 status code is passed.

    Parameters

    Returns this

set

  • set(name: string, value: string): this
  • Set the value of the HTTP header.

    Parameters

    • name: string

      Header name.

    • value: string

      Header value.

    Returns this

setFile

  • setFile(file: FileItem, name?: undefined | string): this
  • Send a file with the response body.

    In this method, it is not required to download the file’s body. The platform will send it to the body of the HTTP response.

    async function downloadFile(req: FetchRequest): Promise<HttpResponse |  void> {
        const file = await System.files.search() 
            .where(f => f. __id.eq('b57ef952-2861-45e1-9dd0-fa5e042ab4ef')).first(); 
        const resp =  new HttpResponse(); 
        resp.setFile(file!); 
        return resp; 
    } 
    

    Parameters

    • file: FileItem

      Link to a FileItem.

    • Optional name: undefined | string

      File name that will be used when the user downloads it.

    Returns this

status

  • Set a response code.

    Parameters

    Returns this