Roxios - Promise-based HttpService wrapper based off of Axios!

roxios

Roxios is a simple, fast, and powerful HttpService wrapper for Roblox, with a focus on mocking the current API in a promise-based format for easy integration into current codebase with little-to-no learning curve.

Roxios was insipired by Axios!

Getting Started

Option 1 - With Wally (Recommended)

To install via Wally, add the following to your wally.toml file:


[dependencies]

roxios = "fxllencode/roxios@1.1.9"

Then, run wally install to install the dependencies.

Option 2 - With .rbxm

You can also drag and drop roxios directly into your project, under ReplicatedStorage.

Head to the releases page to download the latest version, and drag pack.rbxm into ReplicatedStorage.

Option 3 - Building from Source Code with Foreman

If you wish, you can also download the source code and build it yourself. In order for this option to work, you must have Foreman installed.

Firstly, clone the repository:


git clone https://github.com/FxllenCode/roxios.git

Then, enter the directory you created. With Foreman, run:


foreman install

to install the required tools.

Then, run: wally install to install the dependencies.

Finally, build the .rbxm file:


rojo build -o pack.rbxm pack.project.json


Testing a sample project

roxios includes a sample project for your convience. To run, first clone the repository:


git clone https://github.com/FxllenCode/roxios.git

Then, enter the directory you created. With Foreman, run:


foreman install

to install the required tools.

Then, run: wally install to install the dependencies.

Finally, serve the project via Rojo:


rojo serve testing.project.json

Ensure you connect via Roblox Studio, and hit run!

Usage

roxios is not intended to be a major difference from most other HttpService wrappers. However, it is a bit more powerful, and has a few additional features.

Firstly, it uses the promise library to make it easier to work with responses and errors. I suggest you check out the documentation for a full list of features, however to sum it up, you call the function, and then use :andThen() to handle the response, or :catch() to handle errors.


local function myFunction()

    return Promise.new(function(resolve, reject, onCancel)

        somethingThatYields()

        resolve("Hello world!")

        if !somethingThatDoesNotYield() then

            reject("Something went wrong!")

        end

    end)

end

myFunction():andThen(print):catch(print)

Secondly, it has a mock-example of HttpRbxApiService, which is nearly identical to HttpService, however, it can make requests to the Roblox API, which HttpService cannot do without a proxy. For your convince, there is a function called RbxApiRequest(options), which is essentially a mock of HttpService:RequestAsync(options), however it returns a promise, and uses a proxy.

However, you shouldn’t use this in production! There are a multitude of reasons as to why you should not use this, and I will not go into them here. Feel free to continue reading below:

API

roxios.Request(options: Options)

Returns: A promise, which resolves to parsedResponse, rawResponse, or rejects with an error.

roxios.Get(url: string, noCache: boolean?, headers: any?)

  • url: The url to request.

  • noCache: Whether or not to use the cache.

  • headers: Any headers to pass to the request.

Returns: A promise, which resolves to parsedResponse, rawResponse, or rejects with an error.

roxios.Post(url: string, json: string, content_type: Enum.HttpContentType?, compress: boolean?, headers: any?)

  • url: The url to request.

  • json: The json to send.

  • content_type: The content type to request back.

  • compress: Whether or not to compress the request.

  • headers: Any headers to pass to the request.

Returns: A promise, which resolves to parsedResponse, rawResponse, or rejects with an error.

roxios.RbxApiRequest(options: Options)

Returns: A promise, which resolves to parsedResponse, rawResponse, or rejects with an error.

Example


local roxios = require(game.ReplicatedStorage.roxios)

local HttpService = game:GetService("HttpService")

roxios.Request({

    Url = "http://httpbin.org/post",

    Method = "POST",

    Headers = {

        ["Content-Type"] = "application/json",

    },

    Body = HttpService:JSONEncode({ hello = "world" }),

})

    :andThen(function(parsedResponse)

        print(parsedResponse.data)

    end)

    :catch(function(error)

        warn(error)

    end)

roxios.Get("http://httpbin.org/get", true)

    :andThen(function(parsedResponse)

        print(parsedResponse)

    end)

    :catch(function(error)

        warn(error)

    end)

roxios.Post(

    "https://httpbin.org/post",

    HttpService:JSONEncode({ hello = "world" }),

    Enum.HttpContentType.ApplicationJson,

    false

)

    :andThen(function(parsedResponse)

        print(parsedResponse.data)

    end)

    :catch(function(error)

        warn(error)

    end)

roxios.RbxApiRequest({

    Url = "http://setup.roblox.com/version",

    Method = "GET",

    Headers = {

        ["Content-Type"] = "application/json",

    },

})

    :andThen(function(_, response)

        print(response)

    end)

    :catch(function(error)

        warn(error)

    end)

Contributing

This project uses Foreman to install all toolchains. If you wish to contribute, please fork the repository, and build the project yourself. Make sure you run selene and stylua before committing any changes, other wise CI may fail.

Please ensure you have tested code before committing. If you have any issues, please open an issue on the Github repository.

Make sure you have updated the version in wally.toml to the latest SemVer.

License

roxios is available under the terms of the MIT License. Terms and conditions are available in LICENSE.txt or at https://opensource.org/licenses/MIT.

11 Likes

Update - v1.1.6

Changes

  • Added safer JSON Deserializing/failsafe if the response is not in JSON. (ParsedResponse will be nil in this case).
  • Extended the example file.
  • Lints and Code Cleanup

Upgrading

With Wally

Update your wally.toml file to:

[dependencies]

roxios = "fxllencode/roxios@1.1.6"

With .rbxm

Head to the releases page to download the latest version, and drag pack.rbxm into ReplicatedStorage. Ensure you replace your other version of Roxios.

Building from Source Code with Foreman

Inside your git repository, run git pull, or git fetch. Then, run the build command again:

rojo build -o pack.rbxm pack.project.json

No offense, but i dont see the point. You can easily get this functionality using request async. And even if you wanted to incorporate promises, you could just manually make them. I’d love to be proven wrong, I love stuff which make my life easier :slight_smile:

1 Like

That’s truly all it is! It’s intended to be a simple wrapper shortening the process needed to make an API request, with syntax nearly identical to Axios to make it a smooth transition. If you look at the example code, what was this:

-- Remember to set enable HTTP Requests in game settings!
local HttpService = game:GetService("HttpService")
 
local function request()
	local response = HttpService:RequestAsync(
		{
			Url = "http://httpbin.org/post",  -- This website helps debug HTTP requests
			Method = "POST",
			Headers = {
				["Content-Type"] = "application/json"  -- When sending JSON, set this!
			},
			Body = HttpService:JSONEncode({hello = "world"})
		}
	)
 
	-- Inspect the response table
	if response.Success then
		print("Status code:", response.StatusCode, response.StatusMessage)
		print("Response body:\n", response.Body)
	else
		print("The request failed:", response.StatusCode, response.StatusMessage)
	end
end
 
-- Remember to wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
if not success then
	print("Http Request failed:", message)
end

Is now this:

 
 ​local​ roxios ​=​ ​require​(game.​ReplicatedStorage​.​roxios​) 
 ​local​ HttpService ​=​ game:​GetService​(​"​HttpService​"​) 
 ​roxios.​Request​({ 
 ​        Url ​=​ ​"​http://httpbin.org/post​"​, 
 ​        Method ​=​ ​"​POST​"​, 
 ​        Headers ​=​ { 
 ​                [​"​Content-Type​"​] ​=​ ​"​application/json​"​, 
 ​        }, 
 ​        Body ​=​ HttpService:​JSONEncode​({ hello ​=​ ​"​world​"​ }), 
 ​}) 
 ​        :​andThen​(​function​(​parsedResponse​) 
 ​                ​print​(parsedResponse.​data​) 
 ​        ​end​) 
 ​        :​catch​(​function​(​error​) 
 ​                ​warn​(error) 
 ​        ​end​)

Similar, but promised based and safer handling!

2 Likes

Update - v1.1.7

Changes

  • Fixed Git error causing latest update to not push
  • Added stricter Luau types.
  • Lints and Code Cleanup

Upgrading

With Wally

Update your wally.toml file to:

[dependencies]

roxios = "fxllencode/roxios@1.1.7"

With .rbxm

Head to the releases page to download the latest version, and drag pack.rbxm into ReplicatedStorage. Ensure you replace your other version of Roxios.

Building from Source Code with Foreman

Inside your git repository, run git pull, or git fetch. Then, run the build command again:

rojo build -o pack.rbxm pack.project.json

Update - v1.1.8

Changes

  • Fixed support as a Wally package.

Upgrading

With Wally

Update your wally.toml file to:

[dependencies]

roxios = "fxllencode/roxios@1.1.8"

With .rbxm

Head to the releases page to download the latest version, and drag pack.rbxm into ReplicatedStorage. Ensure you replace your other version of Roxios.

Building from Source Code with Foreman

Inside your git repository, run git pull, or git fetch. Then, run the build command again:

rojo build -o pack.rbxm pack.project.json

Hey there, this tool is really amazing !

Just one thing to say :
When trying to use the [?] the character in a header in a POST request using roxios.request, I get errors like that :
Roxios error:
Header “text” has unallowed character “?” in value “[the request] ?”

With normal HTTP request sender (outside Roblox), I can put the ? character in header. Is this a Roxios limitation, or a Roblox-wide one ?

1 Like

Could I see your code? Seems like an issue on my end, I’d like to be able to reproduce it.

Glad you find it useful!

Edit: probably because of missing url encoding on my end. I will fix that later today! :crazy_face:

Hi there, here is my code :

local RequestSender = game.ReplicatedStorage.RequestSender
local HTTPService = game:GetService("HttpService")
local roxios = require(game.ReplicatedStorage.roxios)

local HttpService = game:GetService("HttpService")

local function SendRequest(text)

end


--------------------------------------

local function OnRequest(plr, text)
	local Response
	roxios.Request({

		Url = "[REDACTED]",

		Method = "POST",

		Headers = {

			["Content-Type"] = "application/json",
			["bearer"] = "9634093638";
			["temp"] = "1";
			["type"] = "text-davinci-002";
			["text"] = text
		},

		Body = HttpService:JSONEncode({ hello = "world" }),

	})

	:andThen(function(parsedResponse)
		Response = parsedResponse.responseToSend
		print(parsedResponse.responseToSend)
		return Response

	end)

	:catch(function(error)

		warn(error)

	end)
end

RequestSender.OnServerInvoke = OnRequest

Basically just sends a request to a server on a RemoteFunction that fires with some text from the client.The text goes in the “text” header, which gives me an error if the “?” character is present.

1 Like

Just to confirm - no error when it isn’t present?

Yes, no errors if not present.

1 Like

Can you try URLEncoding the string out of curiosity?

You mean encoding the “text” header ? If I do that with the text “Hi there ?”, it gives me this error :
Roxios error:
Header “text” has unallowed character “%” in value “Hi%20friend%20%3F”

Interesting, and what is the specific error? Gonna try and repo this now.

Okay, I’ve done some investigation, and this is an issue with Roblox and not Roxios, there’s multiple other posts with similar issues. I wish you best, but I believe the issue is when it’s present in headers. Does you use case allow you to send text via request body instead? That’s generally the standard. :grin:

Thanks for the help ! I’ll use the body for the text instead then

1 Like

Update - v1.1.9

Changes

  • Added URL parsing using @cxmeels’s URL package.
  • Added safer URL detection to ensure RbxHttpApiRequest only occurs for a Roblox url.

Upgrading

With Wally

Update your wally.toml file to:

[dependencies]

roxios = "fxllencode/roxios@1.1.9"

With .rbxm

Head to the releases page to download the latest version, and drag pack.rbxm into ReplicatedStorage. Ensure you replace your other version of Roxios.

Building from Source Code with Foreman

Inside your git repository, run git pull, or git fetch. Then, run the build command again:

rojo build -o pack.rbxm pack.project.json
3 Likes

Hey, I know this thread is already two years old, but plan to add typing?