Introduction
I haven’t seen any good tutorials regarding how to use HTTPService, which is a shame given just how cool it really is. Now I’m going to assume most of you reading this will know what HTTP requests are, but for those of you who don’t, HTTP requests can be used to get data from and send data to pretty much anything on the web. HTTP requests come be sent using any of the following request types, GET, POST, PUT, PATCH, and DELETE. HTTP Requests are not just limited websites however, for applications have the capabilities to send HTTP Requests as well, and Roblox games sure do also have the ability to send Http Requests, using HTTPService.
Before we start
For anyone programmers who believe that they don’t need a tutorial like this, but do want to learn about HTTPService, the RobloxStudio API Reference Manual entry for HTTPService is also very helpful
What can we use HTTPService for? (plus an example)
Using HTTPService Roblox games have the ability to send and get information from other websites. You can also make databases (outside of roblox) to use in your Roblox game with HTTPService. For anyone who would like an example check out the Alt Avatar Creation Center and the Alt Avatar Viewing Range. Make an avatar in the Creation Center, save it, and play as that avatar in the Viewing Range, note how the Creation Center does not overwrite your Roblox avatar, yet you can still play as it in a completely different game, this is because it makes clever use of HTTPService.
Getting started with HTTPService
Suppose you want to make a game which includes the caption of the freshest meme on Reddit (because you are just cool like that), but you don’t want that caption to become cringy once it has become a thing of the past, so you want the the meme to constantly update with whatever is on the top of r/memes, if the top meme is overtaken by another meme, ok, just replace it with the caption of whatever is on the top now. So, in order to do this, we obviously need to use GET requests, let’s just test it out. First we need to allow game to have HTTP Requests, to do that go to Game Settings > Security > Allow HTTP Requests, and enable it. Then create a server script in ServerScriptService. We can use the :GetAsync() function to send a GET request to Reddit’s r/memes, then I will print out the result, once you have done that you should end up with this
print(game.HttpService:GetAsync("https://www.reddit.com/r/memes/"))
Run the game and… oh no! It appears as though it has printed all of the HTML data for the r/memes page, this could be helpful for… something I guess but this isn’t really what we are looking for. Thankfully Reddit has our back, by putting “.json” at the end of the link should give us all of the JSON data for the page, yipee! But wait! Roblox uses Lua, so JSON is useless here… not necessarily… HTTPService provides a useful function called :JSONDecode() which transform nasty JSON data into something our lovely Lua script can understand.
local data = game.HttpService:GetAsync("https://www.reddit.com/r/memes/.json")
print(game.HttpService:JSONDecode(data))
Now all we need to do is look for the caption of the top post. We can do that by slightly modifying the code we have written so far:
local data = game.HttpService:GetAsync("https://www.reddit.com/r/memes/.json")
print(game.HttpService:JSONDecode(data).data.children[1].data.title)
Now lets toss in a while loop just so it can update frequently, remember HTTPService will only allow a maximum of 500 requests per minute
while wait(0.2) do
local data = game.HttpService:GetAsync("https://www.reddit.com/r/memes/.json")
print(game.HttpService:JSONDecode(data).data.children[1].data.title)
end
Ta-da!
Going a bit deeper…
So, as you may know by now, you can send GET requests with :GetAsync(). You can also send POST requests, which send info to a website, using :PostAsync(). You can also use :RequestAsync() which is a bit more customizable.
Finally… headers. If you have decided to go on the Roblox API Reference Manual entry for HTTPService, it shouldn’t take you any longer than 2 seconds to notice that “headers” play a massive role when it comes to HTTPService. But what are headers? Headers can be used to send extra information before sending a request, if you plan on using HTTPService a lot, the two pieces of information you will probably be seeing yourself adding to the header a lot is ContentType and Keys, ContentType simply indicates how you are formatting the information sent through a request, and keys are pretty much a passcode on the recipient of the request which decide whether you have permission to send the request or not.
Conclusion
In conclusion, HTTPService opens up the door to so many amazing things which can be done within your Roblox game. If you have any questions, please reply with your question and I will hopefully get back to you. Hope this tutorial helped!