SQL Server
SQL Server
Duplicate curl -u user:password with Chilkat HTTP
See more HTTP Misc Examples
Demonstrates how to duplicate a curl command that uses the -u username:password option. (This assumes HTTP Basic Authentication, and Chilkat requires Basic authentication to be over a TLS connection.)Duplicates the following curl command:
curl https://api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "Client-Id:Secret" \ -d "grant_type=client_credentials"
Chilkat SQL Server Downloads
-- 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)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
-- The AddHeader method corresponds to the curl "-H" argument.
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Accept', 'application/json'
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Accept-Language', 'en_US'
-- The curl "-d" argument specifies the HTTP request body. In this case,
-- we're sending an application/x-www-form-urlencoded request, and therefore
-- the body contains the URL-encoded query parameters.
EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'client_credentials'
EXEC sp_OASetProperty @http, 'Login', 'PAYPAL_REST_API_CLIENT_ID'
EXEC sp_OASetProperty @http, 'Password', 'PAYPAL_REST_API_SECRET'
-- Sends a POST request where the Content-Type is application/x-www-form-urlencoded
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpReq', @success OUT, 'https://api.sandbox.paypal.com/v1/oauth2/token', @req, @resp
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 <> 200
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Error status code: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
RETURN
END
-- The JSON response is in the resp BodyStr property
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
PRINT '-- Success.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
END
GO