Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Example: Http.HttpReq method

Demonstrates how to call the HttpReq method.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local http = chilkat.Http.new()
local success = false

-- This example will send an HTTP POST with the body of the HTTP request containing JSON.

-- Create the following JSON: { "name": "Harry", "state": "FL" }

local json = chilkat.JsonObject.new()
json:update_string("name", "Harry")
json:update_string("state", "FL")

-- Specify the details of the request
local req = chilkat.HttpRequest.new()

req.http_verb = "POST"
req.content_type = "application/json"
req:load_body_from_string(json:emit(), "utf-8")

-- Send the charset attribute in the HTTP request header.
req.send_charset = true
req.charset = "utf-8"

-- Send the POST
-- You can send the POST to the URL below.
-- The response will contain the raw body of the request that was sent.
-- (i.e. everything except the HTTP request header).

local resp = chilkat.HttpResponse.new()
success = http:http_req("https://www.chilkatsoft.com/echo_request_body.asp", req, resp)
if not success then
    print(http.last_error_text)
    return
end

-- Examine the HTTP request header we sent:
print(http.last_header)

-- The response body contains the raw content of the HTTP request body we sent.
print(resp.body_str)

-- Sample output:

-- POST /echo_request_body.asp HTTP/1.1
-- Host: www.chilkatsoft.com
-- Content-Type: application/json; charset=utf-8
-- Content-Length: 29
--
--
-- {"name":"Harry","state":"FL"}