DataFlex
DataFlex
Debug REST HTTP Request
See more REST Examples
Demonstrates how to generate the HTTP Request (with all headers intact) without actually sending the request.Note: This example requires Chilkat v9.5.0.77 or later.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoRest
Boolean iBTls
Integer iPort
Boolean iBAutoReconnect
Handle hoJson
Variant vSbRequestBody
Handle hoSbRequestBody
Variant vSbResponseBody
Handle hoSbResponseBody
Variant vBdRequest
Handle hoBdRequest
String sTemp1
Integer iTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
// Connect Code...
// URL: https://test-api.service.hmrc.gov.uk/organisations/vat/MY_HMRC_VRN/returns
Move True To iBTls
Move 443 To iPort
Move True To iBAutoReconnect
Get ComConnect Of hoRest "test-api.service.hmrc.gov.uk" iPort iBTls iBAutoReconnect To iSuccess
If (iSuccess <> True) Begin
Get ComConnectFailReason Of hoRest To iTemp1
Showln "ConnectFailReason: " iTemp1
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// Build the request body...
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "periodKey" "A001" To iSuccess
Get ComUpdateNumber Of hoJson "vatDueSales" "105.50" To iSuccess
Get ComUpdateNumber Of hoJson "vatDueAcquisitions" "-100.45" To iSuccess
Get ComUpdateNumber Of hoJson "totalVatDue" "5.05" To iSuccess
Get ComUpdateNumber Of hoJson "vatReclaimedCurrPeriod" "105.15" To iSuccess
Get ComUpdateNumber Of hoJson "netVatDue" "100.10" To iSuccess
Get ComUpdateInt Of hoJson "totalValueSalesExVAT" 300 To iSuccess
Get ComUpdateInt Of hoJson "totalValuePurchasesExVAT" 300 To iSuccess
Get ComUpdateInt Of hoJson "totalValueGoodsSuppliedExVAT" 3000 To iSuccess
Get ComUpdateInt Of hoJson "totalAcquisitionsExVAT" 3000 To iSuccess
Get ComUpdateBool Of hoJson "finalised" True To iSuccess
// Add Headers...
Get ComAddHeader Of hoRest "Accept" "application/vnd.hmrc.1.0+json" To iSuccess
Get ComAddHeader Of hoRest "Authorization" "Bearer HMRC_ACCESS_TOKEN" To iSuccess
Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequestBody
If (Not(IsComObjectCreated(hoSbRequestBody))) Begin
Send CreateComObject of hoSbRequestBody
End
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get ComEmitSb Of hoJson vSbRequestBody To iSuccess
// Set DebugMode so that no request is actually sent.
Set ComDebugMode Of hoRest To True
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComFullRequestSb Of hoRest "POST" "/organisations/vat/MY_HMRC_VRN/returns" vSbRequestBody vSbResponseBody To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_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..
Get Create (RefClass(cComChilkatBinData)) To hoBdRequest
If (Not(IsComObjectCreated(hoBdRequest))) Begin
Send CreateComObject of hoBdRequest
End
Get pvComObject of hoBdRequest to vBdRequest
Get ComGetLastDebugRequest Of hoRest vBdRequest To iSuccess
Showln "----"
Get ComGetString Of hoBdRequest "utf-8" To sTemp1
Showln sTemp1
Showln "----"
// 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}
//
//
End_Procedure