Chilkat Online Tools

Android / Anypoint Platform APIs / Login to Anypoint Platform

Back to Collection Items

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http = new CkHttp();
    boolean success;

    CkHttpRequest req = new CkHttpRequest();

    CkJsonObject jsonParam1 = new CkJsonObject();
    req.AddParam("username",jsonParam1.emit());

    CkJsonObject jsonParam2 = new CkJsonObject();
    req.AddParam("password",jsonParam2.emit());

    CkHttpResponse resp = http.PostUrlEncoded("https://domain.com/accounts/login",req);
    if (http.get_LastMethodSuccess() == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    Log.i(TAG, String.valueOf(resp.get_StatusCode()));
    Log.i(TAG, resp.bodyStr());


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

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": [
  ]
}