Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions imports/httpRequest/server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local authorizedMethods = {
['GET'] = true,
['POST'] = true,
['PUT'] = true,
['DELETE'] = true,
['PATCH'] = true,
}

---@async
---@param url string
---@param method? "GET" | "POST" | "PUT" | "DELETE" | "PATCH"
---@param data? string
---@param headers? table
---@param options? table
---@return table | boolean
function lib.httpRequest(url, method, data, headers, options)
method = method or "GET"
headers = headers or {}
options = options or {}

if not authorizedMethods[method] then
lib.print.error(('Invalid HTTP method "%s"'):format(method))
return false
end

local p = promise.new()
PerformHttpRequest(url, function(status, body, responseHeaders, errorData)
p:resolve({
status = status,
body = body,
headers = responseHeaders,
error = errorData,
})
end, method, data, headers, options)

Citizen.Await(p)
return p.value
end

return lib.httpRequest