Chilkat Online Tools

SQL Server / Creatio API / Authentication

Back to Collection Items

-- 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 assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample 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

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "UserName": "{{UserName}}",
    --   "UserPassword": "{{UserPassword}}"
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'UserName', '{{UserName}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'UserPassword', '{{UserPassword}}'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json; charset=utf-8'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'ForceUseSession', 'true'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://myserver.com/ServiceModel/AuthService.svc/Login', 'application/json; charset=utf-8', @json
    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
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

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

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

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

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "Code": 0,
    --   "Message": "",
    --   "Exception": null,
    --   "PasswordChangeUrl": null,
    --   "RedirectUrl": null
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @Code int
    EXEC sp_OAMethod @jResp, 'IntOf', @Code OUT, 'Code'
    DECLARE @Message nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Message OUT, 'Message'
    DECLARE @Exception nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Exception OUT, 'Exception'
    DECLARE @PasswordChangeUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PasswordChangeUrl OUT, 'PasswordChangeUrl'
    DECLARE @RedirectUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @RedirectUrl OUT, 'RedirectUrl'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Accept: application/json"
	-H "Content-Type: application/json; charset=utf-8"
	-H "ForceUseSession: true"
	-d '{  
  "UserName":"{{UserName}}",
  "UserPassword":"{{UserPassword}}"
}'
https://myserver.com/ServiceModel/AuthService.svc/Login

Postman Collection Item JSON

{
  "name": "Authentication",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "pm.variables.set(\"BPMCSRF\", decodeURIComponent(postman.getResponseCookie(\"BPMCSRF\").value));"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Accept",
        "type": "text",
        "value": "application/json"
      },
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json; charset=utf-8",
        "type": "text"
      },
      {
        "key": "ForceUseSession",
        "type": "text",
        "value": "true"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{  \r\n  \"UserName\":\"{{UserName}}\",\r\n  \"UserPassword\":\"{{UserPassword}}\"\r\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{BaseURI}}/ServiceModel/AuthService.svc/Login",
      "host": [
        "{{BaseURI}}"
      ],
      "path": [
        "ServiceModel",
        "AuthService.svc",
        "Login"
      ]
    },
    "description": "Request to the AuthService.svc authentication service."
  },
  "response": [
    {
      "name": "[200] Gets authentication cookies",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Accept",
            "type": "text",
            "value": "application/json"
          },
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json; charset=utf-8",
            "type": "text"
          },
          {
            "key": "ForceUseSession",
            "type": "text",
            "value": "true"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{  \r\n  \"UserName\":\"User01\",\r\n  \"UserPassword\":\"User01\"\r\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://myserver.com/ServiceModel/AuthService.svc/Login",
          "protocol": "https",
          "host": [
            "myserver",
            "com"
          ],
          "path": [
            "ServiceModel",
            "AuthService.svc",
            "Login"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Cache-Control",
          "value": "private"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Location",
          "value": "/0"
        },
        {
          "key": "Server",
          "value": "Microsoft-IIS/10.0"
        },
        {
          "key": "X-Terrasoft-UserCulture",
          "value": "ru-RU"
        },
        {
          "key": "X-AspNet-Version",
          "value": "4.0.30319"
        },
        {
          "key": "Set-Cookie",
          "value": ".ASPXAUTH=D74B814D5E2C4C7B124C94B2AB1E1302F6D5956D6E6FBAE08BB46BCC8EE6A05CC67319EBDA3EFFE035624C7BE0329F584FFBAF9AF70B24BFFEED44115327DE39867DEC44A579BBAF514AF3F5484AAE6CF5753E541F28F5E08A8773A4D9B7DC3C9A805AD9EDCFFA4D9789D80CEA5726760019DF7C5C5929C653C4AD24E77C7945822B89B921A1F16ADF6E0EF65FEDA6CE0FBA6EC830779059575B725C554A44AC1AF966572B75D243722FEB22A497901E6B7717D1CCF192428E52D76B6508A489889C70A1E9F5FEC03011347BAB1CAD3BC3DFA0E36B7B050929176FD43487D189BFFA4034D6D239C3DDE00D81800EA4847CED2C30DDA34F098FBF1E945671863822C1118E63A8F68AB8487E8B3C75EBE5AF7BD41BF7FA8E6C3741CDE6775A135C30708ED6436183357F43DDC2826A54CCA15246B4CF3C0D7C2AE53723590BBAE43B8EA120B73C7700C2AED5589D70877647ADB27159081626D0111F9902ECE4E79B357D0E; path=/; HttpOnly"
        },
        {
          "key": "Set-Cookie",
          "value": "BPMCSRF=Y4s3IaCGUaEwFcgkGKCbH.; path=/"
        },
        {
          "key": "Set-Cookie",
          "value": "UserName=83|117|112|101|114|118|105|115|111|114; expires=Sat, 09-May-2020 11:14:30 GMT; path=/; HttpOnly"
        },
        {
          "key": "X-Powered-By",
          "value": "ASP.NET"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Date",
          "value": "Thu, 09 Apr 2020 11:14:30 GMT"
        },
        {
          "key": "Content-Length",
          "value": "84"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"Code\": 0,\n    \"Message\": \"\",\n    \"Exception\": null,\n    \"PasswordChangeUrl\": null,\n    \"RedirectUrl\": null\n}"
    }
  ]
}