Chilkat Online Tools

unicodeC / GP-API Collection / TRA_10.0 Device Sync

Back to Collection Items

#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;

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

    http = CkHttpW_Create();

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

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

    // {
    //   "account_name": "{{acc_name}}",
    //   "channel": "CP",
    //   "country": "CA",
    //   "device": {
    //     "time": "2019-09-23T12:45:58.000Z"
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"account_name",L"{{acc_name}}");
    CkJsonObjectW_UpdateString(json,L"channel",L"CP");
    CkJsonObjectW_UpdateString(json,L"country",L"CA");
    CkJsonObjectW_UpdateString(json,L"device.time",L"2019-09-23T12:45:58.000Z");

    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
    CkHttpW_SetRequestHeader(http,L"X-GP-Idempotency",L"12348");
    CkHttpW_SetRequestHeader(http,L"X-GP-Version",L"{{version}}");
    // Adds the "Authorization: Bearer {{token}}" header.
    CkHttpW_putAuthToken(http,L"{{token}}");
    CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");

    resp = CkHttpW_PostJson3(http,L"https://{{url}}/ucp/devices",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"%d\n",CkHttpResponseW_getStatusCode(resp));
    wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
    CkHttpResponseW_Dispose(resp);


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);

    }

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Authorization: Bearer {{token}}"
	-H "Accept: application/json"
	-H "X-GP-Version: {{version}}"
	-H "X-GP-Idempotency: 12348"
	-d '{

          "account_name": "{{acc_name}}",
          "channel": "CP",
          "country":"CA",
          "device": {
              "time": "2019-09-23T12:45:58.000Z"
          }
        }'
https://{{url}}/ucp/devices

Postman Collection Item JSON

{
  "name": "TRA_10.0 Device Sync",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "var str = String(\"00000000\"+ Math.floor(Math.random()*100000000) + 1).slice(-8);",
          "",
          "pm.environment.set(\"reference\",str);"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{token}}",
        "type": "text"
      },
      {
        "key": "Accept",
        "value": "application/json",
        "type": "text"
      },
      {
        "key": "X-GP-Version",
        "value": "{{version}}",
        "type": "text"
      },
      {
        "key": "X-GP-Idempotency",
        "value": "12348",
        "type": "text",
        "disabled": true
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n\r\n          \"account_name\": \"{{acc_name}}\",\r\n          \"channel\": \"CP\",\r\n          \"country\":\"CA\",\r\n          \"device\": {\r\n              \"time\": \"2019-09-23T12:45:58.000Z\"\r\n          }\r\n        }"
    },
    "url": {
      "raw": "https://{{url}}/ucp/devices",
      "protocol": "https",
      "host": [
        "{{url}}"
      ],
      "path": [
        "ucp",
        "devices"
      ]
    }
  },
  "response": [
  ]
}