Sample code for 30+ languages & platforms
Lua

POST application/x-www-form-urlencoded using REST API

See more REST Examples

Demonstrates how to send a POST with query params (x-www-form-urlencoded) using the Chilkat REST object.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local rest = chilkat.Rest.new()

-- This example will send to https://www.chilkatsoft.com/echoPost.asp

-- Make the initial connection (without sending a request yet).
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("www.chilkatsoft.com", port, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

-- Provide query params.
rest:add_query_param("firstName", "John")
rest:add_query_param("lastName", "Doe")
rest:add_query_param("company", "Bisco Bits Ltd.")

local responseStr = rest:full_request_form_url_encoded("POST", "/echoPost.asp")
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

-- When successful, the response status code will equal 200.
if rest.response_status_code ~= 200 then
    -- Examine the request/response to see what happened.
    print("response status code = " .. rest.response_status_code)
    print("response status text = " .. rest.response_status_text)
    print("response header: " .. rest.response_header)
    print("response body (if any): " .. responseStr)
    print("---")
    print("LastRequestStartLine: " .. rest.last_request_start_line)
    print("LastRequestHeader: " .. rest.last_request_header)
    return
end

print(responseStr)
print("Success.")