Jump to content

Lua capture & MakeRequest [Basic]


Recommended Posts

Lua's string.gmatch function is a powerful tool for capturing matches in a string using regular expressions. It allows you to iterate over all the matches in a string and extract the relevant information you need.

Basic Usage

The string.gmatch function takes two arguments: the string to search for matches in, and a pattern to match against. The pattern should be a string that specifies the pattern to match using Lua's pattern matching syntax.

local str = "The quick brown fox jumps over the lazy dog"
for word in str:gmatch("%a+") do
--[[ OUTPUT : 

In this example, str:gmatch("%a+") matches any sequence of one or more alphabetic characters in the string str, and the for loop iterates over each match found, printing it to the console.


Capturing Groups

One of the most powerful features of regular expressions is the ability to capture parts of a match using groups. You can specify a group using parentheses in your pattern, and string.gmatch will return the captured text for each match.


local str = "John Doe (30) is a software developer"
for name, age in str:gmatch("(%a+ %a+) %((%d+)%)") do
  print(name, age) --OUTPUT: John Doe	30

In this example, the pattern (%a+ %a+) %((%d+)%) captures two groups: the full name of the person (%a+ %a+ matches two sequences of one or more alphabetic characters separated by a space), and their age (%d+ matches one or more digits enclosed in parentheses). The for loop iterates over each match found, and name and age capture the text of the two groups for each match.


Escaping Special Characters

If you need to match a special character that is also a pattern metacharacter (like "%" or "(" ), you need to escape it using a % character.


local str = "The cost is $19.99"
for cost in str:gmatch("%$%d+%.%d%d") do
  print(cost) -- OUTPUT : $19.99

In this example, the pattern %$%d+%.%d%d matches a dollar sign ($), followed by one or more digits (%d+), a decimal point (%.), and two digits (%d%d). The $ character and the . character are both pattern metacharacters, so they need to be escaped with a % character to be matched literally.


Using anchors and modifiers

In addition to regular expressions, string.gmatch supports the use of anchors and modifiers to further refine your pattern matching.

Anchors are special characters that match the beginning or end of a line or string. The "^" character matches the beginning of a string or line, and the "$" character matches the end of a string or line.


local str3 = "This is line one.\nThis is line two.\nThis is line three."
for line in str3:gmatch("([^\n]*)") do
--[[ OUTPUT : 
This is line one.
This is line two.
This is line three.

In this example the pattern ([^\n]*) to match all the text at the beginning of each line in str3 up to the first newline character.


• The gmatch function takes a pattern as an argument and returns an iterator function that can be used in a for loop to iterate over all the matches in a string.

• You can use capturing groups in your pattern to extract specific parts of the matched text.

• You should escape special characters in your pattern using % to avoid errors and unexpected behavior.

• Anchors like ^ and $ can be used to match the beginning or end of a line or string.

• Modifiers like + and * can be used to match one or more occurrences or zero or more occurrences of the preceding character or group.

• Using string.gmatch with patterns, capturing groups, anchors, and modifiers can help you easily extract relevant information from a string without having to manage the iteration yourself.



Character Class:

A character class is used to represent a set of characters. The following combinations are allowed in describing a character class:

: (where x is not one of the magic characters ^$()%.[]*+-?) represents the character x itself.

: (a dot) represents all characters.

%a : represents all letters.

%c : represents all control characters.

%d : represents all digits.

%g : represents all printable characters except space.

%l : represents all lowercase letters.

%p : represents all punctuation characters.

%s : represents all space characters.

%u : represents all uppercase letters.

%w : represents all alphanumeric characters.

%x : represents all hexadecimal digits.

%x: (where x is any non-alphanumeric character) represents the character x. This is the standard way to escape the magic characters. Any non-alphanumeric character (including all punctuation characters, even the non-magical) can be preceded by a '%' when used to represent itself in a pattern.

[set]: represents the class which is the union of all characters in set. A range of characters can be specified by separating the end characters of the range, in ascending order, with a '-'. All classes %x described above can also be used as components in set. All other characters in set represent themselves. For example, [%w_] (or [_%w]) represents all alphanumeric characters plus the underscore, [0-7] represents the octal digits, and [0-7%l%-] represents the octal digits plus the lowercase letters plus the '-' character. You can put a closing square bracket in a set by positioning it as the first character in the set. You can put a hyphen in a set by positioning it as the first or the last character in the set. (You can also use an escape for both cases.)

The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning.

[^set]: represents the complement of set, where set is interpreted as above.

For all classes represented by single letters (%a, %c, etc.), the corresponding uppercase letter represents the complement of the class. For instance, %S represents all non-space characters.

The definitions of letter, space, and other character groups depend on the current locale. In particular, the class [a-z] may not be equivalent to %l.

Pattern Item:

A pattern item can be

a single character class, which matches any single character in the class;

a single character class followed by '*', which matches zero or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;

a single character class followed by '+', which matches one or more repetitions of characters in the class. These repetition items will always match the longest possible sequence;

a single character class followed by '-', which also matches zero or more repetitions of characters in the class. Unlike '*', these repetition items will always match the shortest possible sequence;

a single character class followed by '?', which matches zero or one occurrence of a character in the class. It always matches one occurrence if possible;

%n, for n between 1 and 9; such item matches a substring equal to the n-th captured string (see below);

%bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.

%f[set], a frontier pattern; such item matches an empty string at any position such that the next character belongs to set and the previous character does not belong to set. The set set is interpreted as previously described. The beginning and the end of the subject are handled as if they were the character '\0'.


A pattern is a sequence of pattern items. A caret '^' at the beginning of a pattern anchors the match at the beginning of the subject string. A '$' at the end of a pattern anchors the match at the end of the subject string. At other positions, '^' and '$' have no special meaning and represent themselves.

Source (3.1 – Patterns).

HTTP(S) Requests:

HTTP (HyperText Transfer Protocol) is a protocol that is used to transmit data over the internet. It is the foundation of the World Wide Web and is used to communicate between clients (such as a web browser) and servers (such as a web server). HTTP requests are used to request resources from a server, such as web pages, images, or other data. Here's a step-by-step guide on how to make HTTP requests:

Step 1: Determine the URL The first step in making an HTTP request is to determine the URL of the resource you want to retrieve. A URL (Uniform Resource Locator) is a unique identifier for a resource on the internet. URLs typically start with either "http://" or "https://" depending on whether the site is using HTTP or HTTPS. For example, the URL of the Google homepage is "https://www.google.com/".

Step 2: Choose an HTTP method HTTP requests use different methods to specify the type of action that is being requested. The most common HTTP methods are:

GET: Retrieve data from the server

POST: Submit data to the server

Choose the appropriate HTTP method depending on the action you want to perform.

Step 3: Construct the request Once you have determined the URL and the HTTP method, you need to construct the HTTP request. An HTTP request consists of a request line, headers, and an optional request body. The request line includes the HTTP method, the URL, and the HTTP version. The headers provide additional information about the request, such as the type of data being sent and the user agent making the request. The request body is used to send data to the server, such as form data or a JSON payload.

Step 4: Send the request Once you have constructed the HTTP request, you can send it to the server using an HTTP client. HTTP clients can be built into programming languages, such as Python's requests library, or they can be standalone tools, such as cURL or Postman. When you send the request, the server will respond with an HTTP response.

Step 5: Handle the response The HTTP response includes a status code, headers, and a response body. The status code indicates whether the request was successful or if there was an error. The headers provide additional information about the response, such as the content type and length. The response body contains the data that was requested or an error message if the request failed. You can handle the response based on the status code and the content of the response body.


HTTP Methods:

GET Method: The GET method is used to retrieve data from the server. When a GET request is made, the server sends back a response with the requested data. The data can be in any format such as HTML, JSON, XML, etc.

Example: To retrieve the homepage of a website using a GET request, you would send a request to the server using the GET method like this:

GET / HTTP/1.1
Host: www.example.com

POST Method: The POST method is used to send data to the server to be processed. This data can be in any format such as form data, JSON, XML, etc. The server processes the data and sends back a response.

Example: To submit a form to a server using the POST method, you would send a request to the server like this:

POST /submit_form.php HTTP/1.1
Host: www.example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 27

name=John&[email protected]


HTTP Response Codes:

HTTP response codes are used to indicate the status of the server's response to the request. There are many HTTP response codes, but the most common ones are:

200 OK: This response code indicates that the request was successful and the server was able to provide the requested data.

201 Created: This response code indicates that the request was successful and a new resource has been created on the server.

400 Bad Request: This response code indicates that the request was malformed and could not be processed by the server.

401 Unauthorized: This response code indicates that the request requires authentication and the user is not authenticated.

404 Not Found: This response code indicates that the requested resource could not be found on the server.

500 Internal Server Error: This response code indicates that there was an error on the server while processing the request.

Response Content:

The response content is the data that is sent back by the server in response to the request. The content can be in any format such as HTML, JSON, XML, etc. The content is usually sent in the body of the HTTP response.


If you make a GET request to the homepage of a website, the server may send back an HTML response like this:

HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8

<!DOCTYPE html>
    <title>Welcome to Example.com</title>
    <h1>Welcome to Example.com</h1>
    <p>This is the homepage of Example.com.</p>

If you make a POST request to submit a form, the server may send back a JSON response like this:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8

  "status": "success",
  "message": "Form submitted successfully"

HTTP/S POST and GET methods, HTTP response codes, and response content are crucial elements in making HTTP requests. Understanding these concepts is essential.


The makeRequest function is a built-in function in the GameGuardian Lua API that can be used to perform GET or POST requests over HTTP or HTTPS. Here's how you can use this function:


response = makeRequest(string url, table headers = {}, string data = nil)


url: A string with the URL to request.

headers: A table with request headers. The key is the name, and the value is a table or a string. If it's a table, then the keys are ignored, and the values are used.

data: A string with data for the POST request. If you specify nil, then there will be a GET request.


Return Values

On success: A table with the result of the request.

On error: A string with the error text.


--> The result table can contain the following fields:

url: The requested URL.

requestMethod: The HTTP method used for the request.

code: The HTTP response code.

message: The HTTP response message.

headers: A table with all the response headers. Each value is also a table, with numeric keys. Usually, there is only one value, but if the header has met several times, such as 'Set-Cookie', then there may be several values.

contentEncoding: Content encoding.

contentLength: Content length.

contentType: Content type.

date: Date of the response.

expiration: Expiration date of the response.

lastModified: Last modified date of the response.

usingProxy: Whether the request used a proxy or not.

error: True or false. True if the server returned an invalid code.

content: A string of data from the server. Can be empty.



Here are some examples of how to use the makeRequest function:


-- 1 ) Simple GET request:
print('GET 1:', makeRequest('http://httpbin.org/headers').content)

-- 2 ) GET request with headers:
print('GET 2:', makeRequest('http://httpbin.org/headers', {['User-Agent'] = 'My BOT'}).content)

-- 3 ) GET request with multiple headers:
print('GET 3:', makeRequest('http://httpbin.org/headers', {['User-Agent'] = {'My BOT', 'Tester'}}).content)

-- 4 ) HTTPS GET request with headers:
print('GET 4:', makeRequest('https://httpbin.org/get?param1=value2&param3=value4', {['User-Agent'] = 'My BOT'}).content)

-- 5 ) Simple POST request:
print('POST 1:', makeRequest('http://httpbin.org/post', nil, 'post1=val2&post3=val4').content)

-- 6 ) POST request with headers:
print('POST 2:', makeRequest('http://httpbin.org/post', {['User-Agent'] = 'My BOT'}, 'post1=val2&post3=val4').content)

-- 7 ) POST request with multiple headers:
print('POST 3:', makeRequest('http://httpbin.org/post', {['User-Agent'] = {'My BOT', 'Tester'}}, 'post1=val2&post3=val4').content)

-- 8 ) HTTPS POST request with headers:
print('POST 4: ', gg.makeRequest('https://httpbin.org/post?param1=value2&param3=value4', {['User-Agent']='My BOT'}, 'post1=val2&post3=val4').content)
This example performs a POST request over HTTPS to the URL https://httpbin.org/post with query parameters param1=value2&param3=value4. It also includes a custom header User-Agent with the value My BOT.

-- 9 ) Full request information:
print('FULL: ', gg.makeRequest('https://httpbin.org/headers'))
--This example performs a GET request over HTTPS to the URL https://httpbin.org/headers and prints out all the information returned by the server.

We typically utilize the function gg.makeRequest to retrieve raw data, however, its application across multiple websites for web scraping or executing POST/GET requests can prove beneficial in various use cases.

Example I want to capture urls in github to use a depandecy on my script such as JSON I'm using POSTMAN to get the request.

--->Similar to [response = gg.makeRequest('https://github.com/rxi/json.lua') -> response.content ]

Here I make a GET request to get the github repo web page.

then I use dev mode to get the html links and titles : 


<a class="js-navigation-open Link--primary" title="json.lua" data-turbo-frame="repo-content-turbo-frame" href="/rxi/json.lua/blob/master/json.lua">json.lua</a>

the Pattern to capture all the <a contents </a> : "<a.-title="(%g+)".-href="(%g+)".-</a>"
So we will capture all the titles with links in this example:


BASE_URL = 'https://github.com'
    local response = gg.makeRequest(BASE_URL..'/rxi/json.lua') -- we concatenate the path with the json repo
     if not response then
        gg.alert('Connection Error !') -- error handller
        if response.code == 200 then -- get 200 OK response [No error]
            for title , link in response.content:gmatch('<a.-title="(%g+)".-href="(%g+)".-</a>') do -- we captue all the titles with a link
                print(title , link)


1151560217_Screenshot_20230318-061147_SamsungExperienceHome.thumb.jpg.10b820887a49a98e6e0b3cb1798b24c0.jpg   --> here we capture all the titles with href links in the request html.

You got the basic Idea! 

You can say to me we can just we make a request to a raw.github etc but this method is great for any website that have no raw data so we filter it using gmatch() and we create the raw data for our-self.

HERE is a video and a template script on usage of gg.makeRequest with ChatGPT[advanced] : 

requestgpt.lua  Note the key will be deleted in 3 days from this post submited date ( 3/18/2023 )

• (this guide can be updated)

Edited by XEKEX
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.