HTTP

HTTP is not creatable, there's only one instance of it. It can only be accessed through its globally exposed variable.

HTTP can be used to send HTTP requests.

Functions

nil Delete ( string url, function callback )
nil Delete ( string url, table headers, function callback )

Sends a DELETE HTTP request to the provided url.
The request is asynchronous, not blocking execution.
Response can be obtained through callback function parameter.
Optionally, a table with request headers can be provided after the url parameter.

local url = "https://jsonplaceholder.typicode.com/posts/1" -- public test API

HTTP:Delete(url, function(res)
  if res.StatusCode ~= 200 then
    print("Error " .. res.StatusCode)
    return
  end
  print("Post successfully deleted!")
end)
nil Get ( string url, function callback )
nil Get ( string url, table headers, function callback )

Sends a GET HTTP request to the provided url.
The request is asynchronous, not blocking execution.
Response can be obtained through callback function parameter.
Optionally, a table with request headers can be provided after the url parameter.

local url = "https://jsonplaceholder.typicode.com/users" -- public test API

HTTP:Get(url, function(res)
  if res.StatusCode ~= 200 then
    print("Error " .. res.StatusCode)
    return
  end
  -- body contains array of user objects
  users,err = JSON:Decode(res.Body)
  local user = users[1]
  print(user.id, user.name, user.email)
  -- prints 1 Leanne Graham [email protected]
end)
nil Patch ( string url, table body, function callback )
nil Patch ( string url, string body, function callback )
nil Patch ( string url, Data body, function callback )
nil Patch ( string url, table headers, table body, function callback )
nil Patch ( string url, table headers, string body, function callback )
nil Patch ( string url, table headers, Data body, function callback )

Sends a PATCH HTTP request to the provided url.
The request is asynchronous, not blocking execution.
If body is a table it gets JSON encoded, otherwise it is sent as a string or raw Data.
Response can be obtained through callback function parameter.
Optionally, a table with request headers can be provided after the url parameter.

local url = "https://jsonplaceholder.typicode.com/posts/1" -- public test API
local headers = {}
headers["Content-Type"] = "application/json"

local body = {}
body.title = "updated title"

HTTP:Patch(url, headers, body, function(res)
  if res.StatusCode ~= 200 then
    print("Error " .. res.StatusCode)
    return
  end
  -- response contains updated post
  post,err = JSON:Decode(res.Body)
  print(post.id, post.title)
  -- prints 1 updated title
end)
nil Post ( string url, table body, function callback )
nil Post ( string url, string body, function callback )
nil Post ( string url, Data body, function callback )
nil Post ( string url, table headers, table body, function callback )
nil Post ( string url, table headers, string body, function callback )
nil Post ( string url, table headers, Data body, function callback )

Sends a POST HTTP request to the provided url.
The request is asynchronous, not blocking execution.
If body is a table it gets JSON encoded, otherwise it is sent as a string or raw Data.
Response can be obtained through callback function parameter.
Optionally, a table with request headers can be provided after the url parameter.

local url = "https://jsonplaceholder.typicode.com/posts" -- public test API
local headers = {}
headers["Content-Type"] = "application/json"

local body = {}
body.title = "foo"
body.body = "bar"
body.userId = 1

HTTP:Post(url, headers, body, function(res)
  if res.StatusCode ~= 201 then
    print("Error " .. res.StatusCode)
    return
  end
  -- response contains created post
  post,err = JSON:Decode(res.Body)
  print(post.id, post.title, post.body, post.userId)
  -- prints 101 foo bar 1
end)