LUA API v2 HTTP Requests - xAranaktu/FC-25-Live-Editor GitHub Wiki
Simple module allowing you to send HTTP requests. Bye bye os.execute("curl")
Under the hood it's using libcpr which (in thereory) means that everything libcpr can do, this lua code can do too.
This class doesn't have any fields.
Sends HTTP Request
Declaration
HTTPResponse HTTP:send(HTTPRequest request)
Parameters
Type | Name | Description |
---|---|---|
HTTPRequest | request | Request to send |
Returns
Simple GET Request
local req = REQUEST:new({
method = HTTP_GET_REQUEST,
url = "https://example.com/",
timeout = 10000 -- 1s
})
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
Set Cookies
local req = REQUEST:new()
req:SetMethod(HTTP_GET_REQUEST)
req:SetUrl("http://www.httpbin.org/cookies/set?cookies=yummy")
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
local cookies = {
["cookie1_key"] = "cookie1_val",
["cookie2_key"] = "cookie2_val"
}
req:SetCookies(cookies)
{
"cookies": {
"cookies": "yummy"
}
}
local req = REQUEST:new()
local cookies = {
["cookie1_key"] = "cookie1_val",
["cookie2_key"] = "cookie2_val"
}
req:SetMethod(HTTP_GET_REQUEST)
req:SetUrl("http://www.httpbin.org/cookies/set?cookies=yummy")
req:SetCookies(cookies)
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
{
"cookies": {
"cookie1_key": "cookie1_val",
"cookie2_key": "cookie2_val",
"cookies": "yummy"
}
}
Set Headers
local req = REQUEST:new()
local headers = {
["user-agent"] = "FC Live Editor"
}
req:SetMethod(HTTP_GET_REQUEST)
req:SetUrl("http://httpbin.org/user-agent")
req:SetHeaders(headers)
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
{
"user-agent": "FC Live Editor"
}
Authentication
local req = REQUEST:new()
local basic_auth = {
["username"] = "user",
["password"] = "pass",
["auth_mode"] = HTTP_AUTH_METHOD_BASIC
}
req:SetMethod(HTTP_GET_REQUEST)
req:SetAuth(basic_auth)
req:SetUrl("http://www.httpbin.org/basic-auth/user/pass")
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
{
"authenticated": true,
"user": "user"
}
local req = REQUEST:new()
local digest_auth = {
["username"] = "user",
["password"] = "pass",
["auth_mode"] = HTTP_AUTH_METHOD_DIGEST
}
req:SetMethod(HTTP_GET_REQUEST)
req:SetAuth(digest_auth)
req:SetUrl("http://www.httpbin.org/digest-auth/auth/user/pass")
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
{
"authenticated": true,
"user": "user"
}
local req = REQUEST:new()
local bearer_token_auth = {
["access_token"] = "ACCESS_TOKEN",
["auth_mode"] = HTTP_AUTH_METHOD_BEARER
}
req:SetMethod(HTTP_GET_REQUEST)
req:SetAuth(bearer_token_auth)
req:SetUrl("http://www.httpbin.org/bearer")
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
{
"authenticated": true,
"token": "ACCESS_TOKEN"
}
Simple Post Request
json = require 'imports/external/json'
local req = REQUEST:new()
local payload = {
["some key"] = "some value",
["another key"] = "another value"
}
req:SetMethod(HTTP_POST_REQUEST)
req:SetPayload(payload)
req:SetUrl("http://httpbin.org/post")
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
-- You can convert response text to json too
local resp_json = json.decode(resp.text)
print(resp_json.form["some key"])
{
"args": {},
"data": "",
"files": {},
"form": {
"another key": "another value",
"some key": "some value"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "deflate, gzip",
"Content-Length": "49",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": "",
"Host": "httpbin.org",
"User-Agent": "curl/7.86.0",
"X-Amzn-Trace-Id": ""
},
"json": null,
"origin": "0.0.0.0",
"url": "http://httpbin.org/post"
}
Raw Body Post
local req = REQUEST:new()
local headers = {
["Content-Type"] = "text/plain"
}
req:SetMethod(HTTP_POST_REQUEST)
req:SetHeaders(headers)
req:SetBody("This is a raw body")
req:SetUrl("http://httpbin.org/post")
local resp = HTTP:send(req)
assert(resp.status_code == 200, string.format("respstatus_code %d != 200", resp.status_code))
print(resp.text)
{
"args": {},
"data": "This is a raw body",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "deflate, gzip",
"Content-Length": "18",
"Content-Type": "text/plain",
"Cookie": "",
"Host": "httpbin.org",
"User-Agent": "curl/7.86.0",
"X-Amzn-Trace-Id": ""
},
"json": null,
"origin": "0.0.0.0",
"url": "http://httpbin.org/post"
}