Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples
Web API Categories

AI
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
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
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)
JavaScript
MHT / HTML Email
MIME
Markdown
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
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) curl POST with JSON Input and JSON Output

See more CURL Examples
Demonstrates running a simple curl command with JSON input and JSON output.

Note: This example requires Chilkat v11.5.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSbTargetCurl
    Handle hoHttpCurl
    Variant vResponseJson
    Handle hoResponseJson
    Integer iStatusCode
    String sTargetCurl
    String sTemp1

    Move False To iSuccess

    // Run the following curl command

    //  curl -X POST https://httpbin.org/post \
    //       -H "Content-Type: application/json" \
    //       -d '{
    //             "title": "foo",
    //             "body": "bar",
    //             "userId": 1
    //           }'

    // The backslashes at the end of lines are not required.  Chilkat ignores them if present.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbTargetCurl
    If (Not(IsComObjectCreated(hoSbTargetCurl))) Begin
        Send CreateComObject of hoSbTargetCurl
    End
    Get ComAppendLn Of hoSbTargetCurl " curl -X POST https://httpbin.org/post \" To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '      -H "Content-Type: application/json" \' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl "      -d '{" To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '            "title": "foo",' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '            "body": "bar",' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl '            "userId": 1' To iSuccess
    Get ComAppendLn Of hoSbTargetCurl "          }'" To iSuccess

    Get Create (RefClass(cComChilkatHttpCurl)) To hoHttpCurl
    If (Not(IsComObjectCreated(hoHttpCurl))) Begin
        Send CreateComObject of hoHttpCurl
    End

    // Run the curl command.
    Get ComGetAsString Of hoSbTargetCurl To sTemp1
    Get ComDoYourThing Of hoHttpCurl sTemp1 To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoResponseJson
    If (Not(IsComObjectCreated(hoResponseJson))) Begin
        Send CreateComObject of hoResponseJson
    End
    Set ComEmitCompact Of hoResponseJson To False

    Get pvComObject of hoResponseJson to vResponseJson
    Get ComGetResponseJson Of hoHttpCurl vResponseJson To iSuccess

    Get ComStatusCode Of hoHttpCurl To iStatusCode
    Showln "response status code: " iStatusCode

    Get ComEmit Of hoResponseJson To sTemp1
    Showln sTemp1

    // Output:

    // response status code: 200
    // {
    //   "args": {},
    //   "data": "{\r\n            \"title\": \"foo\",\r\n            \"body\": \"bar\",\r\n            \"userId\": 1\r\n          }",
    //   "files": {},
    //   "form": {},
    //   "headers": {
    //     "Content-Length": "96",
    //     "Content-Type": "application/json",
    //     "Host": "httpbin.org",
    //     "X-Amzn-Trace-Id": "Root=1-69e8db8b-459b3bdf7b7a3bc749184968"
    //   },
    //   "json": {
    //     "body": "bar",
    //     "title": "foo",
    //     "userId": 1
    //   },
    //   "origin": "123.222.222.222",
    //   "url": "https://httpbin.org/post"
    // }

    // ----------------------------------------------------------------------------------
    // Another example:

    // curl -X POST https://postman-echo.com/post \
    //      -H "Content-Type: application/json" \
    //      -d '{"foo":"bar"}'

    Move 'curl -X POST https://postman-echo.com/post -H "Content-Type: application/json" -d '{"foo":"bar"}'' To sTargetCurl

    // Run the curl command.
    Get ComDoYourThing Of hoHttpCurl sTargetCurl To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttpCurl To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoResponseJson to vResponseJson
    Get ComGetResponseJson Of hoHttpCurl vResponseJson To iSuccess

    Get ComStatusCode Of hoHttpCurl To iStatusCode
    Showln "response status code: " iStatusCode

    Get ComEmit Of hoResponseJson To sTemp1
    Showln sTemp1

    // Output:

    // response status code: 200
    // {
    //   "args": {},
    //   "data": {
    //     "foo": "bar"
    //   },
    //   "files": {},
    //   "form": {},
    //   "headers": {
    //     "host": "postman-echo.com",
    //     "content-length": "13",
    //     "content-type": "application/json",
    //     "x-forwarded-proto": "https",
    //     "accept-encoding": "gzip, br"
    //   },
    //   "json": {
    //     "foo": "bar"
    //   },
    //   "url": "https://postman-echo.com/post"
    // }


End_Procedure

 

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