Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add HttpRequest method #2

Merged
merged 6 commits into from
Oct 20, 2021
Merged

feat: add HttpRequest method #2

merged 6 commits into from
Oct 20, 2021

Conversation

r-o-b-o-t-o
Copy link
Member

This PR adds the possibility to perform HTTP/HTTPS requests in a non-blocking way (they are queued up and executed one by one in a separate thread). The requests can be executed via the HttpRequest global function.

Example usage, from the function's docs:

-- GET example (prints a random word)
HttpRequest("GET", "https://random-word-api.herokuapp.com/word", function(status, body, headers)
    print("Random word: " .. string.sub(body, 3, body:len() - 2))
end)

-- POST example with JSON request body
HttpRequest("POST", "https://jsonplaceholder.typicode.com/posts", '{"userId": 1,"title": "Foo","body": "Bar!"}', "application/json", function(status, body, headers)
    print(body)
end)

-- Example with request headers
HttpRequest("GET", "https://postman-echo.com/headers", { Accept = "application/json", ["User-Agent"] = "Eluna Lua Engine" }, function(status, body, headers)
    print(body)
end)

The following 3rd party libraries are used by this PR:

Another example usage:
(this example requires json.lua)

local json = require("json")

RegisterPlayerEvent(42, function(event, player, command)
    if command == "joke" then
        local guid = player:GetGUID()

        HttpRequest("GET", "https://api.chucknorris.io/jokes/random", function(status, body, headers)
            local player = GetPlayerByGUID(guid)
            res = json.decode(body)
            player:SendBroadcastMessage(res.value)
        end)
        return false
    end
end)

Result:
httprequestexamples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants