Chilkat Online Tools

SQL Server / Anypoint Platform APIs / Login to Anypoint Platform

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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.HttpRequest', @req OUT

    DECLARE @jsonParam1 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam1 OUT

    EXEC sp_OAMethod @jsonParam1, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'username', @sTmp0

    DECLARE @jsonParam2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonParam2 OUT

    EXEC sp_OAMethod @jsonParam2, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'password', @sTmp0

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostUrlEncoded', @resp OUT, 'https://domain.com/accounts/login', @req
    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 @req
        EXEC @hr = sp_OADestroy @jsonParam1
        EXEC @hr = sp_OADestroy @jsonParam2
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @jsonParam1
    EXEC @hr = sp_OADestroy @jsonParam2


END
GO

Curl Command

curl -X POST
	--data-urlencode 'username={{username}}'
	--data-urlencode 'password={{password}}'
https://domain.com/accounts/login

Postman Collection Item JSON

{
  "name": "Login to Anypoint Platform",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "const jar = pm.cookies.jar();\r",
          "\r",
          "jar.clear(pm.request.url, function (error) {\r",
          "\r",
          "});\r",
          ""
        ],
        "type": "text/javascript"
      }
    },
    {
      "listen": "test",
      "script": {
        "exec": [
          "var data = JSON.parse(responseBody);\r",
          "pm.environment.set(\"token\", data.access_token);\r",
          "pm.environment.set(\"cookies\", \"_csrf=\" + pm.cookies.get(\"_csrf\") +\r",
          "                              \"; XSRF-TOKEN=\" + pm.cookies.get(\"XSRF-TOKEN\") +\r",
          "                              \"; mulesoft.sess=\" + pm.cookies.get(\"mulesoft.sess\") +\r",
          "                              \"; mulesoft.sess.sig=\" + pm.cookies.get(\"mulesoft.sess.sig\") );\r",
          "                              \r",
          "pm.cookies.jar().clear(pm.environment.get(\"url\"));\r",
          "const cookieJar = pm.cookies.jar();\r",
          "cookieJar.clear(pm.environment.get(\"url\"));"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "username",
          "value": "{{username}}",
          "type": "text"
        },
        {
          "key": "password",
          "value": "{{password}}",
          "type": "text"
        }
      ],
      "options": {
        "urlencoded": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{url}}/accounts/login",
      "host": [
        "{{url}}"
      ],
      "path": [
        "accounts",
        "login"
      ]
    }
  },
  "response": [
  ]
}