The res
object represents the HTTP response that a server sends when it gets an HTTP request.
In this documentation and by convention, the object is always referred to as res
(and the HTTP request is req
) but its actual name is determined by the parameters to the callback function in which you’re working.
For example:
mock.get("/user/{id}", function (req, res) {
res.send("user " + req.params.id);
});
But you could just as well have:
mock.get("/user/{id}", function (request, response) {
response.send("user " + request.params.id);
});
The res
object contains a Writer
field which is a golang's http.ResponseWriter instance.
• append: (field
: string, value
: string) => Response
Appends the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value.
param
the header field name
param
the value to append
returns
The instance for fluent/chaining API
▸ (field
: string, value
: string): Response
Name | Type |
---|---|
field |
string |
value |
string |
Returns: Response
• binary: (body
: string | number[] | ArrayBuffer) => Response
Sends a binray response. This method sends a response (with the "application/octet-stream" content-type) that is the body paramter.
param
the data to send
returns
The instance for fluent/chaining API
▸ (body
: string | number[] | ArrayBuffer): Response
Name | Type |
---|---|
body |
string | number[] | ArrayBuffer |
Returns: Response
• html: (body
: string) => Response
Sends a HTML text response. This method sends a response (with the correct content-type) that is the body string paramter.
param
the string to send
returns
The instance for fluent/chaining API
▸ (body
: string): Response
Name | Type |
---|---|
body |
string |
Returns: Response
• json: (body
: Record<string, any>) => Response
Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string.
param
the object to send
returns
The instance for fluent/chaining API
▸ (body
: Record<string, any>): Response
Name | Type |
---|---|
body |
Record<string, any> |
Returns: Response
• redirect: (code
: number, loc
: string) => Response
Redirects to the URL, with specified status, a positive integer that corresponds to an HTTP status code.
param
the HTTP status code (301, 302, ...)
param
the location to redirect
returns
The instance for fluent/chaining API
▸ (code
: number, loc
: string): Response
Name | Type |
---|---|
code |
number |
loc |
string |
Returns: Response
• send: (body
: string | number[] | ArrayBuffer) => Response
Sends the HTTP response.
When the parameter is a ArrayBuffer or number[], the method sets the Content-Type response header field to “application/octet-stream”. When the parameter is a String, the method sets the Content-Type to “text/html”. Otherwise the method sets the Content-Type to "application/json" and convert paramter to JSON representation before sending.
param
the data to send
returns
The instance for fluent/chaining API
▸ (body
: string | number[] | ArrayBuffer): Response
Name | Type |
---|---|
body |
string | number[] | ArrayBuffer |
Returns: Response
• set: (field
: string, value
: string) => Response
Sets the response’s HTTP header field to value.
param
the header field name
param
the value to set
returns
The instance for fluent/chaining API
▸ (field
: string, value
: string): Response
Name | Type |
---|---|
field |
string |
value |
string |
Returns: Response
• status: (code
: number) => Response
Sets the HTTP status for the response.
param
the satus code value
returns
The instance for fluent/chaining API
▸ (code
: number): Response
Name | Type |
---|---|
code |
number |
Returns: Response
• text: (format
: string, v?
: any[]) => Response
Sends a plain text response. This method sends a response (with the correct content-type) that is the string formatting result.
param
go format string
param
format values (if any)
returns
The instance for fluent/chaining API
▸ (format
: string, v?
: any[]): Response
Name | Type |
---|---|
format |
string |
v? |
any[] |
Returns: Response
• type: (mime
: string) => Response
Sets the Content-Type HTTP header to the MIME type as from mime parameter.
params
mime the content type
returns
The instance for fluent/chaining API
▸ (mime
: string): Response
Name | Type |
---|---|
mime |
string |
Returns: Response
• vary: (header
: string) => Response
Adds the header field to the Vary response header.
param
the header filed name
returns
The instance for fluent/chaining API
▸ (header
: string): Response
Name | Type |
---|---|
header |
string |
Returns: Response