Lua Requires Chilkat v9.5.0.77+
Lua
Debug REST HTTP Request
See more REST Examples
Demonstrates how to generate the HTTP Request (with all headers intact) without actually sending the request.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- This example will connect to the web server, but does not actually send a request.
-- When in DebugMode, the request is composed in memory and can be retrieved by calling
-- GetLastDebugRequest.
local rest = chilkat.Rest.new()
-- Connect Code...
-- URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("test-api.service.hmrc.gov.uk", port, bTls, bAutoReconnect)
if not success then
print("ConnectFailReason: " .. rest.connect_fail_reason)
print(rest.last_error_text)
return
end
-- Build the request body...
local json = chilkat.JsonObject.new()
json:update_string("periodKey", "A001")
json:update_number("vatDueSales", "105.50")
json:update_number("vatDueAcquisitions", "-100.45")
json:update_number("totalVatDue", "5.05")
json:update_number("vatReclaimedCurrPeriod", "105.15")
json:update_number("netVatDue", "100.10")
json:update_int("totalValueSalesExVAT", 300)
json:update_int("totalValuePurchasesExVAT", 300)
json:update_int("totalValueGoodsSuppliedExVAT", 3000)
json:update_int("totalAcquisitionsExVAT", 3000)
json:update_bool("finalised", true)
-- Add Headers...
rest:add_header("Accept", "application/vnd.hmrc.1.0+json")
rest:add_header("Authorization", "Bearer HMRC_ACCESS_TOKEN")
rest:add_header("Content-Type", "application/json")
local sbRequestBody = chilkat.StringBuilder.new()
json:emit_sb(sbRequestBody)
-- Set DebugMode so that no request is actually sent.
rest.debug_mode = true
local sbResponseBody = chilkat.StringBuilder.new()
success = rest:full_request_sb("POST", "/organisations/vat/MY_HMRC_VRN/returns", sbRequestBody, sbResponseBody)
if not success then
print(rest.last_error_text)
return
end
-- Get the exact contents of what would've been sent.
-- This includes the HTTP start line, the HTTP request headers, and the request body.
-- Given that it's possible for the request body to contain binary data,
-- the GetLastDebugRequest fetches into a BinData object.
-- In this case, however, our request body contained JSON, so we can
-- examine it as a string..
local bdRequest = chilkat.BinData.new()
success = rest:get_last_debug_request(bdRequest)
print("----")
print(bdRequest:get_string("utf-8"))
print("----")
-- The output for the above case:
-- POST /organisations/vat/MY_HMRC_VRN/returns HTTP/1.1
-- Accept: application/vnd.hmrc.1.0+json
-- Host: test-api.service.hmrc.gov.uk
-- Authorization: Bearer HMRC_ACCESS_TOKEN
-- Content-Type: application/json
-- Content-Length: 281
--
-- {"periodKey":"A001","vatDueSales":105.50, ... ,"finalised":true}
--
--