Lightweight HTTP server and client library

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)
21 Likes

really good job!

1 Like

Great work, only thing, is there a way for me to use this from a client ui
Rn i have a personal resource which uses http, but fivem says its not secure.

I did not know about that, thank you. However, this is only minor part of this “library” and single function for synchronous request.

1 Like

I’m not sure about that. Depends on some NUI restrictions.

PERFECT!
I’ve been looking for exactly this for a few days, it came at the perfect time

1 Like