How do I use HttpService

HTTP service is basically just making web requests between the Roblox game server and external websites, like @JAcoboiskaka1121 said.

First of all, the name. HTTP stands for Hypertext Transfer Protocol, which is a protocol in networking used to transfer plaintext and HTML (Hypertext Markup Language) webpages between computers.

Understanding the 4 main HTTP Request Methods (I’ve skipped PUT)

GET

This is used only to retrieve information from a web resource; it doesn’t update the resource as it goes. It’s generally pretty safe compared to the other methods. It’s idempotent, meaning all identical requests will have the same result if they succeed.

POST

This submits data to the server in order to create or update a resource. You send data in the request body and it’s processed by the server. It’s not idempotent, different succeeding identical requests will have different results.

PATCH

This is used to partially update an already existing resource. It only changes the specified fields within the resource and nothing else. It’s inconsistently idempotent, identical requests can have the same result but not always.

DELETE

This is used to delete a resource from the server. Not idempotent.


Request structure

Data for a request will typically have 3-4 fields in it. (going off of HttpService’s RequestAsync for reference). These fields are:

  • Url - the URL of the requested website
  • Method - the HTTP method the request should use
  • Headers - Data about the request, describes what’s in the Body and also can contain authentication material
  • Body - Data for the request itself

Data formats in the Body

There are many different forms a HTTP body can take, but the most common one is JSON, or JavaScript Object Notation. All of these forms are standards so the websites can communicate information. Websites typically use JavaScript to handle their logic, which is likely why JSON is so common.


Setting up a simple HTTP request

Let’s take an example of unbanning a user in a Roblox game. HTTP service can’t access Roblox API, but for the sake of this example, let’s imagine it can.

local httpService = game:GetService("HttpService")
local apiKey = "" --API key for authentication!

--let's create the request data
local data = {
    ["Url"] = "https://apis.roblox.com/cloud/v2/universes/UNIVERSE_ID/user-restrictions/USER_ID",
    ["Method"] = "DELETE", --we want to delete the ban entry
    ["Headers"] = {
        ["x-api-key"] = apiKey --include our authentication as request metadata
    }
}

--we send the request via RequestAsync
local success, result = pcall(httpService.RequestAsync, httpService, data)

When sending POST, PATCH or PUT requests, you must include the Content-Type header. It should always be application/ or text/, and then your data type (respective of whether it’s application or text), for example this for JSON:

{
    ["Content-Type"] = "application/json"
}

I know this post was pretty long, but I hope it helps! Lmk if u have any questions.

1 Like