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.
returnnew 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.
Server response
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
Methods
content
Set the format of the response body content to string.
Parameters
data: string
Returns this
cookie
Set the value of a cookie.
Parameters
name: string
value: string
Optional options: CookieOptions
Returns this
json
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 headingContent-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 happyAs
data, you can pass an instance object in which thejson()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
Returns this
location
Set
Locationas the HTTP header.Parameters
url: string
Returns this
redirect
Redirect request. By default, the 302 status code is passed.
Parameters
url: string
Optional status: HttpStatusCode | number
Returns this
set
Set the value of the HTTP header.
Parameters
name: string
value: string
Returns this
setFile
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
Optional name: undefined | string
Returns this
status
Set a response code.
Parameters
statusCode: HttpStatusCode | number
Returns this
Feedback sent successfully
Thank you for your input