Roblox Requests: Robust HTTP Library for Roblox

Roblox Requests

Roblox Requests is a simple and elegant yet powerful HTTP library, based on the well known Python Requests.

local r = http.get("https://api.github.com/orgs/Roblox/repos")

print(r.status_code, r.message)
-- 200 OK

repos = r:json()
print(#repos)
-- 30

print(r.content_type)
-- application/json
print(r.encoding)
-- utf-8
print(r.headers["x-ratelimit-remaining"])
-- 59

The main purpose of Requests is to avoid the manual labor of HttpService.
Common issues I’ve had with HttpService include manually specifying the content type and formatting URL queries.

Here’s some code written using Roblox Requests:

local http = require(game.ReplicatedStorage.http)

local r = http.post("https://httpbin.org/post", { query = {name="value"},
                                                  data = {this="isjson"} })

print(r.content_type)
print(r.url)
-- output:
-- application/json
-- https://httpbin.org/post?arg=value

local data = r:json()

Compare this to the same code written using HttpService:

local HttpService = game:GetService("HttpService")

local options = {
	Url = "https://httpbin.org/post?arg=value",
	Method = "POST",
	Headers = {["Content-Type"] = "application/json"},
	Body = HttpService:JSONEncode( {this="isjson"} )
}

local r = HttpService:RequestAsync(options)

print(r.Headers["content-type"])

local data = HttpService:JSONEncode(r.Body)

This isn’t all that Requests is good for. Here’s some other features that will make your life easier:

  • Functions for every HTTP verb
  • Sessions with cookie persistence, base URLs/headers
    • Configurable rate-limiting to help you balance your game’s HTTP usage
  • Automatic query string building, JSON body encoding
  • Accessible Response object with useful data, builtin JSON decoding
  • Multipart form building, including file encoding and upload
  • Domain based key/value cookies
  • Builtin support for Promises

You can find full documentation at http://requests.paric.xyz/. If you have any suggestions or bug reports, feel free to let me know here or contribute on the GitHub page!
27 Likes

Promise Support

Roblox Requests now supports Roblox Lua Promises. I think this addition will make the library more useful to many Roblox devs looking for a more robust solution for HTTP requests in Roblox.

Here’s an example of using the library’s existing methods with Promises:

http.promise_get("https://api.github.com/orgs/this_org_does_not_exist/repos")
    :andThen(function(response)
        for _, repo in ipairs(response:json()) do
            print(repo.name)
        end
    end)
    :catch(function(err)
        if err.request_sent then
            print("HTTP Error:", err.response.status_code, err.response.message)
        else
            print(err.error)
        end
    end)

    -- HTTP Error: 404 Not Found

For more info on using Promises, see the project documentation:
http://requests.paric.xyz/guide/promises/

Full changelog and module download:

1 Like