Request
Request is a class that represents a single HTTP request. The idea behind this class is to provide some sort of structure and boilerplate for making HTTP requests.
by default, this class will add the following headers:
- User-Agent: “DiscordLuau”
- Authorization: “Bot <token>”
- Content-Type: “application/json”
Summary
Methods
Request:setBody(body: string)Request:setFlag(name: string, value: boolean)Request:addUrlParam(paramName: string, paramValue: string | { string })Request:addHeader(headerName: string, headerValue: string)Request:setUrl(url: string)Request:setMethod(method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE")Request:assertToken()Request:addHook(hookState: "BeforeRequest" | "AfterRequest", hookCallback: (RequestInformation, stdPolyfills.FetchResponse?) -> (boolean, any?))Request:executeAsync()Methods
Section titled “Methods”setBody
Section titled “setBody”Request : setBody ( body ) -> ()
Responsible for setting the Body of this request. Body must be a string.
Parameters
| body: string |
setFlag
Section titled “setFlag”Request : setFlag ( name , value ) -> ()
Allows us to define flags for this request, used to flag requests that could potentially contain attachments. and, therefore need to be translated into formdata before we push anything.
addUrlParam
Section titled “addUrlParam”Request : addUrlParam ( paramName , paramValue ) -> ()
Responsible for adding to the Url Params of this request. Values may be a string or an array of strings;
arrays are serialized as repeated query parameters (e.g. key=a&key=b) with full URL encoding applied
by the underlying runtime’s net.request implementation.
Parameters
| paramName: string | |
| paramValue: string | { string } |
addHeader
Section titled “addHeader”Request : addHeader ( headerName , headerValue ) -> ()
Responsible for adding to the Headers of this request. Both the header key and values are strings.
You are not allowed to set the following headers:
- User-Agent
- Authorization
setUrl
Section titled “setUrl”Request : setUrl ( url ) -> ()
Responsible for setting the URL of this request. URL must be a string, and formatted like so:
Parameters
| url: string |
setMethod
Section titled “setMethod”Request : setMethod ( method ) -> ()
Responsible for setting the Method of this request. Method must be one of GET, POST, PUT, PATCH, or DELETE.
Methods define the nature of the request, and the response.
Parameters
| method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" |
assertToken
Section titled “assertToken”Request : assertToken ( ) -> ()
Responsible for validating the Authorization token of this request. Some calls require token authorization, and some do not, so the relevant calls should assert that the token is set.
addHook
Section titled “addHook”Request : addHook ( hookState , hookCallback ) -> ()
Enables developers to add a hook to this request. Hooks are functions that are called before or after the request is executed. Hooks are useful for things like logging, or modifying the request before it is executed.
The hook callback should return a boolean, and if it returns false, the request will not be executed.
Parameters
| hookState: "BeforeRequest" | "AfterRequest" | |
| hookCallback: FetchResponse?) -> (boolean, any?) |
executeAsync
Section titled “executeAsync”Request : executeAsync ( ) -> ()
Responsible for executing this request asynchronously. This will return a async that will resolve when the request is completed.
This function will decode the response body, and return a table with the following keys:
- headers: A table of headers returned from the request.
- body: The body of the request, decoded from the response.
Functions
Section titled “Functions”Request . new ( settings ) -> Request
Constructor for the Request object.