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

POST JSON to REST API with non-us-ascii Chars Escaped

See more REST Examples

Demonstrates how to POST to a REST API with non-usascii chars within JSON Unicode escaped.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

local rest = chilkat.Rest.new()

-- Connect using TLS.
local bAutoReconnect = true
success = rest:connect("chilkatsoft.com", 443, true, bAutoReconnect)

-- Load JSON containing the following Korean text.

--  {
--    "BillAddr": {
--       "Id": "239615",
--       "Line1": "류리하",
--       "Line2": "류리하류리하",
--       "City": "류리하류리하",
--       "Country": "US",
--       "CountrySubDivisionCode": "AK",
--       "PostalCode": "류리하"
--     }
-- }

local json = chilkat.JsonObject.new()
json.emit_compact = false
success = json:load_file("qa_data/json/korean.json")
if not success then
    print(json.last_error_text)
    return
end

success = rest:add_header("Content-Type", "application/json; charset=UTF-8")

local sb = chilkat.StringBuilder.new()
json:emit_sb(sb)
sb:encode("unicodeescape", "utf-8")

print(sb:get_as_string())

-- The StringBuilder contains this:

-- {
--   "BillAddr": {
--     "Id": "239615",
--     "Line1": "\ub958\ub9ac\ud558",
--     "Line2": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
--     "City": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
--     "Country": "US",
--     "CountrySubDivisionCode": "AK",
--     "PostalCode": "\ub958\ub9ac\ud558"
--   }
-- }

local sbResp = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/echo_request_body.asp", sb, sbResp)
if not success then
    print(rest.last_error_text)
    return
end

-- Show the response.
print("Json Response: " .. sbResp:get_as_string())