GSmGpE6CwX2y9JjB25B8
We use cookies on this site to enhance your user experience

Sending HTTP Requests

Sending HTTP Requests

Aug 29 2019, 9:24 AM PST 5 min

This article requires basic knowledge of Lua, Roblox Studio, and how to use the Internet.

HttpService allows you to send and receive data from websites through HTTP requests. It also provides a few utility methods for using JSON, a very common data interchange format used on the Internet today. HttpService is great for moving game data on/off Roblox, but it’s also commonly used to send data to external programs which are not a part of Roblox Studio.

What is HTTP?

When you visit a website, your web browser sends something called an HTTP request. Essentially, it represents a message from your computer: “Hey, could I have this page?” to the target web server, specified by a URL (Uniform Resource Locator). The server then creates a response and sends it back to your computer. The typical response is a web page written using a markup language called HTML, short for Hypertext Markup Language. It looks like this:

<html>
	<head>
		<title>The Title of the Webpage</title>
	</head>
	<body>
		The content of the document
	</body>
</html>

In most browsers, like Chrome, Firefox or Edge, you can press CTRL+U to view the HTML source of a webpage. However, not all URLs will give HTML! There are many that provide data that provide website functionality; for example this URL leads to a page on Roblox’s website which provides the user ID of the account currently logged in: http://www.roblox.com/game/GetCurrentUser.ashx Pages like these are often called endpoints. You can make HTTP requests to URLs like these using HttpService to send/receive data in/out of your game. In the following sections, we’ll discuss the different ways that HTTP requests can be made.

GetAsync and HTTP GET

When you are only visiting a website and not submitting any data, your browsers sends an HTTP GET request. This is a request that just means “send me information (don’t do anything else)”. This is what HttpService/GetAsync does: Roblox sends a simple request for information. When a script calls this, it will wait until either a) a response is received or b) the request times out, which happens when the target server takes too long to respond.

local HttpService = game:GetService("HttpService")
local response = HttpService:GetAsync("http://api.open-notify.org/iss-now.json", true)
print(response)

When you run the above example code, Roblox will make a GET request to the target URL (http://api.open-notify.org/iss-now.json), which provides data about the location of the International Space Station. Neat!

PostAsync and HTTP POST

When you submit information through a website, your browser sends an HTTP POST request. These are special requests that have the data you’re submitting attached in the “body” of the request (which is empty for GET requests). The method works much like HttpService/GetAsync|GetAsync, except that your script can decide on the POST data.

local HttpService = game:GetService("HttpService")
local postData = "Hi, this is the HTTP POST data!"
local response = HttpService:PostAsync("http://httpbin.org/post", postData)
print(response)

The example above will make an HTTP POST request to the website httpbin.org, which provides a number of URLs that help you find bugs in web applications. It does this by sending information about your request right back to you. So, the above example will print out information about our POST request, the body of which should include our postData above.

What’s JSON?

JSON, or JavaScript Object Notation, is a data-interchange format. It is based off JavaScript, which is a scripting language all browsers use for webpages. It describes data structures that are used in almost all modern programming languages, which have libraries that can read and write JSON. Roblox Lua is no different! Much like HTML, it is a human-readable data format and it is easy to write:

{
	"key": "value",
	"score": 5,
	"enabled": true,
	"array": [1, 2, 3],
	"object": {
		"message": "JSON example!"
	}
}

It is strikingly similar to Lua tables! In fact, Lua tables can be turned directly into JSON by using HttpService/JSONEncode (e.g. HttpService:JSONEncode({1,2,3}) will return [1,2,3]). You can turn this JSON string back into a Lua table by using HttpService/JSONDecode. Beware, though: if there’s a syntax error in your JSON (such as a trailing comma or unfinished string), this method will raise an error! Many web endpoints, like the ones in the previous section, use JSON to send/receive data in HTTP GET and POST requests.

HTTP vs HTTPS

Security is essential in today’s world. Many websites support HTTPS, which is a secure version of HTTP that prevents eavesdropping and other malicious actions. Your browser will show “https://www.roblox.com” and usually a green lock icon to indicate the security behind such websites. If you are sending data that must be secure, check that your target URL supports an HTTPS equivalent. Some website endpoints require that you only use HTTPS, so always check their documentation for more information. All the same, no matter which you use you won’t need to change the way you use HttpService.

External Programs to Roblox Studio

You need not make requests to URLs on the Internet when using HttpService. It is entirely possible to program a web server on your computer using PHP, Node.js, Python, Ruby or Go, then run it while using Studio. A script on Roblox can communicate with such a web server to perform tasks normally not possible in Roblox Studio, like working with your computer’s file system. An example of such a tool is Rojo.

Enabling HTTP Requests

Enabling HTTP requests for HttpService is straightforward: In Studio, locate the top menu bar. Press Insert -> Service -> HttpService. Select HttpService in the Explorer window and find HttpService/HttpEnabled in the Properties window. Check the box to set this true, and you’re ready to send HTTP requests!

Restrictions

  • HttpService cannot access Roblox domains. However, it is possible to set up a proxy server to make requests to the main Roblox website on your game’s behalf.
  • You can only make 500 requests per minute. Exceeding this will lock up the service for 30 seconds, so be mindful!
  • You must have “http://” or “https://” at the beginning of your URL.

Common errors

“HTTP requests are not enabled”

You must explicitly allow HTTP requests to be made in a place: HttpService has a property called HttpService/HttpEnabled|HttpEnabled which must be set to true in order to allow requests to be made. Select HttpService in the Explorer window, then find HttpEnabled in the Properties window. Tick the box to enable requests.

“Http requests can only be executed by a game server”

LocalScript|LocalScripts, which run code on a player’s computer, cannot make HTTP requests. Instead, the Roblox game server they are connecting to needs to make them on players’ behalf. You can use RemoteFunction and RemoteEvent to allow a LocalScript to request that the server make an HTTP request on its behalf, but you should take care to prevent spamming such requests (e.g. for requests triggered by a button, use a debounce).

“trust check failed”

There are two possible reasons you got this error:

  • You did not include “http://” or “https://” at the beginning of your URL
  • You are trying to access a website that is disallowed, such as a Roblox website.

See also

  • HttpService

Tags:
  • http
  • get
  • post