Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Debug REST HTTP Request

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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Boolean iSuccess
    Handle hoJson
    Variant vSbRequestBody
    Handle hoSbRequestBody
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Variant vBdRequest
    Handle hoBdRequest
    String sTemp1
    Integer iTemp1

    // 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

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.