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

SQL Server 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
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
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)
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

 

 

 

(SQL Server) POST application/x-www-form-urlencoded Two Ways

This example explains how a POST with params can be formatted in different ways. Params can be encoded in the URL, in which case they appear in the "start line" of the HTTP request, or params can be placed within the body of the request. A properly implemented server SHOULD accept both -- it SHOULD make no difference where the params are located. They are simply the params of a POST. But alas, many servers are not, and we need to cope with the particulars..

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- We start with this CURL statement:

    -- curl -X POST "https://xyz.softeon.com/api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB" -H "Content-Type: application/x-www-form-urlencoded"

    -- Postman would send the following request:

    -- POST /api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB HTTP/1.1
    -- Content-Type: application/x-www-form-urlencoded
    -- User-Agent: PostmanRuntime/7.26.8
    -- Accept: */*
    -- Cache-Control: no-cache
    -- Postman-Token: 719e6ada-5b0e-4d3f-8166-37433efdcaa1
    -- Host: xyz.softeon.com
    -- Accept-Encoding: gzip, deflate, br
    -- Connection: keep-alive
    -- Content-Length: 0

    -- We can see that the params are located in the "start line" of the HTTP request, and the request has no body (the Content-Length is 0).

    -- You can use Chilkat's online tool to generate the code for the above CURL statement.
    -- Generate Source Code from CURL Statement
    -- 

    -- Here's the generated code:

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- We can see the exact HTTP request sent by setting a session log filename:
    EXEC sp_OASetProperty @http, 'SessionLogFilename', 'qa_output/sessionLog.txt'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/x-www-form-urlencoded'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'QuickRequest', @resp OUT, 'POST', 'https://xyz.softeon.com/api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB'
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        RETURN
      END


    PRINT 'Response body:'
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @resp

    -- Here's what is sent by Chilkat.  It is essentially the same thing:

    -- POST /api/token?grant_type=client_credentials&resource=https://xyz.softeon.com/resources&scope=openid&client_id=AAA&client_secret=BBB HTTP/1.1
    -- Host: xyz.softeon.com
    -- Accept: */*
    -- Accept-Encoding: gzip
    -- Content-Type: application/x-www-form-urlencoded
    -- Content-Length: 0

    -- -------------------------------------------------------------------------------------------------------------------------------------------------
    -- Now let's look at the alternative way of sending a POST application/x-www-form-urlencoded,
    -- where the params are contained within the body of the request.

    -- curl -X POST  https://xyz.softeon.com/api/token \
    -- -H 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' \
    -- -d 'grant_type=client_credentials' \
    -- -d 'resource=https://xyz.softeon.com/resources' \
    -- -d 'scope=openid' \
    -- -d 'client_id=AAA' \
    -- -d 'client_secret=BBB'

    -- The Chilkat tool at https://tools.chilkat.io/curlHttp.cshtml
    -- generates the following code:
    -- (We changed the names of the variables to keep things entirely separate from the above code.)

    DECLARE @httpB int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @httpB OUT

    EXEC sp_OASetProperty @httpB, 'SessionLogFilename', 'qa_output/sessionLogB.txt'

    DECLARE @reqB int
    -- Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @reqB OUT

    EXEC sp_OASetProperty @reqB, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @reqB, 'Path', '/api/token'
    EXEC sp_OASetProperty @reqB, 'ContentType', 'application/x-www-form-urlencoded'
    EXEC sp_OAMethod @reqB, 'AddParam', NULL, 'grant_type', 'client_credentials'
    EXEC sp_OAMethod @reqB, 'AddParam', NULL, 'resource', 'https://xyz.softeon.com/resources'
    EXEC sp_OAMethod @reqB, 'AddParam', NULL, 'scope', 'openid'
    EXEC sp_OAMethod @reqB, 'AddParam', NULL, 'client_id', 'AAA'
    EXEC sp_OAMethod @reqB, 'AddParam', NULL, 'client_secret', 'BBB'

    DECLARE @respB int
    EXEC sp_OAMethod @httpB, 'PostUrlEncoded', @respB OUT, 'https://xyz.softeon.com/api/token', @reqB
    EXEC sp_OAGetProperty @httpB, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @httpB, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @httpB
        EXEC @hr = sp_OADestroy @reqB
        RETURN
      END


    PRINT 'Response body:'
    EXEC sp_OAGetProperty @respB, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @respB

    -- Looking at the sessionLogB.txt, we can see the following HTTP POST was sent:

    -- POST /api/token HTTP/1.1
    -- Host: xyz.softeon.com
    -- Content-Type: application/x-www-form-urlencoded
    -- Content-Length: 134
    -- 
    -- grant_type=client_credentials&resource=https%3A%2F%2Fxyz.softeon.com%2Fresources&scope=openid&client_id=AAA&client_secret=BBB

    -- --
    -- Notice how the params are sent in the body of the request.  The Content-Length is now non-zero, and the params are not present in the start-line of the HTTP request.
    -- Technically, this SHOULD be equivalent to the above request where the params are in the start-line.
    -- Unfortunately, many servers are brittle and stupid and want the POST to be in a particular form.

    EXEC @hr = sp_OADestroy @respB


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @httpB
    EXEC @hr = sp_OADestroy @reqB


END
GO

 

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