URL encoding is a standard mechanism for converting special characters in a URL to a format that can be transmitted over the internet. It is often necessary to decode URL-encoded strings in Lua to retrieve the original values.

In this tutorial, we will walk through the process of decoding a URL-encoded string in Lua.


Before we begin, you should have a basic understanding of Lua programming and string manipulation.

Step 1: Install Required Package

To decode URL-encoded strings in Lua, we will need to install the url package. Open your terminal and run the following command to install the package using LuaRocks:

luarocks install url

Step 2: Import the ‘url’ Package

In your Lua script, import the url package using the require keyword:

local url = require(""url"")

Step 3: URL Decode the String

Now, let’s decode a URL-encoded string using the url.unescape function provided by the url package. Here’s an example:

local encodedString = ""Hello%20World%21""
local decodedString = url.unescape(encodedString)


The output will be:

Hello World!

Step 4: Handle Errors

In some cases, the URL-encoded string may be malformed or contain invalid characters. To handle such scenarios, it’s a good practice to wrap the decoding logic in a pcall function:

local success, decodedString = pcall(url.unescape, encodedString)

if success then
    print(""Failed to decode URL-encoded string."")

This ensures that your script doesn’t crash in case of errors during the decoding process.


URL decoding is an essential task when working with URLs and web-related data in Lua. By following this guide, you have learned how to decode URL-encoded strings using the url package in Lua.

I hope you found this tutorial helpful!