Skip to content

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”

Properties

The Request instance has no set properties!

Methods

The Request instance has no set methods!

Functions

setBody

Request . setBody (

     self: Request
     body: string
)  -> ()

Responsible for setting the Body of this request. Body must be a string.

setFlag

Request . setFlag (

     self: Request
     name: string
     value: boolean
)  -> ()

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

Request . addUrlParam (

     self: Request
     paramName: string
     paramValue: string
)  -> ()

Responsible for adding to the Url Params of this request. Both the param key and values are strings.

addHeader

Request . addHeader (

     self: Request
     headerName: string
     headerValue: string
)  -> ()

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

Request . setUrl (

     self: Request
     url: string
)  -> ()

Responsible for setting the URL of this request. URL must be a string, and formatted like so:

setMethod

Request . setMethod (

     self: Request
     method: "GET"
                      | "POST"
                      | "PUT"
                      | "PATCH"
                      | "DELETE"
)  -> ()

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.

assertToken

Request . assertToken (

     self: Request
)  -> ()

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

Request . addHook (

     self: Request
     hookState: "BeforeRequest" | "AfterRequest"
     hookCallback: (RequestInformation, stdPolyfills.FetchResponse?) -> any
)  -> ()

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.

executeAsync

Request . executeAsync (

     self: Request
)  -> ()

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.

new

Request . new (

     settings: {
       token: string?,
       restApiVersion: number,
    }
)  -> ()

Constructor for the Request object.