Gives app scripts access to the current HTTP request and response, and to reading and writing cookies. An instance is bound as the 'http' script global for the duration of a request, so scripts read it as http.request, http.response, http.getCookie(...) and http.setCookie(...) rather than constructing it directly.
Group: Managers
Properties
| Property | Returns | Description |
|---|---|---|
| request | Request | The current HTTP request, for example to read a cookie from it: http.request.getCookie("return-url"). |
| requestBody | String | Reads the current request's body as text, buffering it on the request so repeated calls do not re-read the underlying input stream. |
| response | Response | The current HTTP response, for example to set a cookie on it: http.response.setCookie("return-url", "/somewhere/i/belong"). |
Methods
getRequest()
Returns: Request
The current HTTP request, for example to read a cookie from it: http.request.getCookie("return-url").
getResponse()
Returns: Response
The current HTTP response, for example to set a cookie on it: http.response.setCookie("return-url", "/somewhere/i/belong").
getRequestBody()
Returns: String
Reads the current request's body as text, buffering it on the request so repeated calls do not re-read the underlying input stream.
setCookie(String name, Object value)
Returns: void
Sets a cookie on the current response with the root path and an expiry of one week. Equivalent to calling setCookie(name, value, 60 * 60 * 24 * 7, "/").
| Parameter | Description |
|---|---|
name | the name of the cookie; must be a valid cookie name |
value | the value to record on the cookie, converted to a string |
setCookie(String name, String value, int expirySecs, String path)
Returns: void
Sets a cookie on the current response with a chosen expiry and path. The cookie's SameSite attribute is always set to Strict.
| Parameter | Description |
|---|---|
name | the name of the cookie; must be a valid cookie name |
value | the value to record on the cookie |
expirySecs | the number of seconds the cookie will remain valid for |
path | the URL path scope the cookie applies to |
setCookie(String name, String value, int expirySecs, String path, String sameSite)
Returns: void
Sets a cookie on the current response with a chosen expiry, path and SameSite value. The sameSite argument is currently not applied to the cookie; the cookie is set with no explicit SameSite attribute.
| Parameter | Description |
|---|---|
name | the name of the cookie; must be a valid cookie name |
value | the value to record on the cookie |
expirySecs | the number of seconds the cookie will remain valid for |
path | the URL path scope the cookie applies to |
sameSite | the intended SameSite value - None, Lax or Strict |
cookieBuilder(String name)
Returns: CookieBuilder
Starts a fluent builder for a cookie, defaulting its SameSite attribute to Strict. Call build() on the returned builder to add the finished cookie to the current response.
| Parameter | Description |
|---|---|
name | the name of the cookie; must be a valid cookie name |
getCookie(String name)
Returns: String
Reads the value of a cookie sent with the current request.
| Parameter | Description |
|---|---|
name | the name of the cookie to look up |