cfx-http 
Lightweight HTTP server and client library designed for the CitizenFX framework (such as FiveM). It allows developers to easily create and manage HTTP servers within their scripts, as well as send HTTP requests to external services. With built-in support for GET and POST methods, route handling, and optional authorization mechanisms, cfx-http simplifies HTTP communication in your projects. It also provides synchronous and asynchronous request capabilities, making it flexible for various use cases.
Repository
Usage
Setting Up an HTTP Server
This example sets up an HTTP server that handles a GET request at /status and a POST request at /data, with optional authorization for the POST route.
local server = exports['cfx-http']:http().server()
-- Handle GET request at "/status" route
server.get("/status", function(req, res)
res(200, "Server is running!", { ["Content-Type"] = "text/plain" })
end)
-- Handle POST request at "/data" route with authorization
server.post("/data", function(req, res)
local requestBody = json.decode(req.body)
-- Process incoming data
...
res(...)
end, "Bearer my_secret_token")
-- Start the server
server.build()
Builder
This example demonstrates how to use the builder pattern with method chaining, allowing you to define routes and then build the server in one fluent flow.
local http = exports['cfx-http']:http()
http.server()
.get("/status", function(req, res)
res(200, "Server is running!", { ["Content-Type"] = "text/plain" })
end)
.post("/data", function(req, res)
local requestBody = json.decode(req.body)
-- Process incoming data
...
res(...)
end, "Bearer my_secret_token")
.build()
HTTP Requests as a Client
In this example, the client sends a GET request to an external URL and a POST request with JSON data.
local http = exports['cfx-http']:http()
local response = http.get("https://yourserver:30120/<resource>/status")
local response = http.get("https:/google.com")
print("Response Status: " .. response.status)
print("Response Body: " .. response.body)
local http = exports['cfx-http']:http()
local response = http.post("https://yourserver:30120/<resource>/setdata", {
["Content-Type"] = "application/json"
})
print("Response Status: " .. response.status)
print("Response Body: " .. response.body)
Asynchronous Requests
For asynchronous use, you can provide a callback to handle the response when it’s ready.
local http = exports['cfx-http']:http()
http.post("https://api.example.com/data", {
["Content-Type"] = "application/json"
}, function(response)
print("Async Response Status: " .. response.status)
print("Async Response Body: " .. response.body)
end)