How to use HTTPService to do amazing things!

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! :wink:

49 Likes

Yes!

I personally used HTTPService to use custom databases instead of datastoreservice.

Very helpful to be able to make external API requests.

Thank you for providing the communitiy with thie great resource.

3 Likes

@Codename_Planet good tutorial however I did find some issues.

You should include pcalls within the code samples.

This won’t work since HttpService has a maximum request limit of 500 a minute. You code uses 600.

You should also talk about the limits to this service (such as the request limit, the fact that it can’t make requests to roblox.com and that requests can sometimes fail so pcalls should be used)

7 Likes

Why not just use

local httpService = game:GetService("HttpService")
local httpService = game:GetService("HttpService")

while wait(10) do
	local data  = httpService:GetAsync("https://www.reddit.com/r/memes/.json")
	print(httpService:JSONDecode(data).data.children[1].data.title)
end

Also why are you calling to update every .1 seconds? You’re going to hit the limit extremely fast that way.

3 Likes

Because that was what was in the OP.

1 Like

Sorry, I meant to reply to OP :frowning:

1 Like

Hey, nice tutorial its short and detailed. I haven’t used HTTPService much yet so I’ll for sure come back to this.

1 Like

Great tutorial, I’m gonna consider adding this to my future plans :+1:

2 Likes

Very useful, thanks bro. I need to learn about HTTPSERVICE more, this helps a lot bro. :grin::+1:

I completely forgot about the 500 request limit, lemme update that in my post

2 Likes

Yeah, completely forgot about the limit, updated the original post

3 Likes

How can i find more examples like “data.title”?

1 Like

Nice tutorial, but i honestly do not like HTTPServices-

2 Likes

Good tutorial I read about 5 months ago and I didn’t know about that Reddit thing either!

1 Like