Chilkat Online Tools

unicodeC / Cognite API v1 / Create assets

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.

    // {
    //   "items": [
    //     {
    //       "name": "anim aliquip cupidatat commodo laborum",
    //       "externalId": "veniam in reprehenderit culpa",
    //       "parentId": 7287302321534466,
    //       "parentExternalId": "nostrud pariatur aute",
    //       "description": "nisi incididunt",
    //       "dataSetId": 1616183313071840,
    //       "metadata": {},
    //       "source": "fugiat ",
    //       "labels": [
    //         {
    //           "externalId": "eu tempor aliqua ipsum quis"
    //         },
    //         {
    //           "externalId": "laborum est aliquip magna"
    //         }
    //       ]
    //     },
    //     {
    //       "name": "in culpa ullamco fugiat p",
    //       "externalId": "qui quis anim ",
    //       "parentId": 4737890210897342,
    //       "parentExternalId": "in",
    //       "description": "id Excepteur volu",
    //       "dataSetId": 6713921295205546,
    //       "metadata": {},
    //       "source": "nostrud",
    //       "labels": [
    //         {
    //           "externalId": "dolor consequat fugiat nisi"
    //         },
    //         {
    //           "externalId": "dol"
    //         }
    //       ]
    //     }
    //   ]
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"items[0].name",L"anim aliquip cupidatat commodo laborum");
    CkJsonObjectW_UpdateString(json,L"items[0].externalId",L"veniam in reprehenderit culpa");
    CkJsonObjectW_UpdateInt(json,L"items[0].parentId",123);
    CkJsonObjectW_UpdateString(json,L"items[0].parentExternalId",L"nostrud pariatur aute");
    CkJsonObjectW_UpdateString(json,L"items[0].description",L"nisi incididunt");
    CkJsonObjectW_UpdateInt(json,L"items[0].dataSetId",123);
    CkJsonObjectW_UpdateNewObject(json,L"items[0].metadata");
    CkJsonObjectW_UpdateString(json,L"items[0].source",L"fugiat ");
    CkJsonObjectW_UpdateString(json,L"items[0].labels[0].externalId",L"eu tempor aliqua ipsum quis");
    CkJsonObjectW_UpdateString(json,L"items[0].labels[1].externalId",L"laborum est aliquip magna");
    CkJsonObjectW_UpdateString(json,L"items[1].name",L"in culpa ullamco fugiat p");
    CkJsonObjectW_UpdateString(json,L"items[1].externalId",L"qui quis anim ");
    CkJsonObjectW_UpdateInt(json,L"items[1].parentId",123);
    CkJsonObjectW_UpdateString(json,L"items[1].parentExternalId",L"in");
    CkJsonObjectW_UpdateString(json,L"items[1].description",L"id Excepteur volu");
    CkJsonObjectW_UpdateInt(json,L"items[1].dataSetId",123);
    CkJsonObjectW_UpdateNewObject(json,L"items[1].metadata");
    CkJsonObjectW_UpdateString(json,L"items[1].source",L"nostrud");
    CkJsonObjectW_UpdateString(json,L"items[1].labels[0].externalId",L"dolor consequat fugiat nisi");
    CkJsonObjectW_UpdateString(json,L"items[1].labels[1].externalId",L"dol");

    CkHttpW_SetRequestHeader(http,L"content-type",L"application/json");
    CkHttpW_SetRequestHeader(http,L"api-key",L"{{api-key}}");

    resp = CkHttpW_PostJson3(http,L"https://domain.com/api/v1/projects/{{project}}/assets",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 "api-key: {{api-key}}"
	-H "content-type: application/json"
	-d '{
    "items": [
        {
            "name": "anim aliquip cupidatat commodo laborum",
            "externalId": "veniam in reprehenderit culpa",
            "parentId": 7287302321534466,
            "parentExternalId": "nostrud pariatur aute",
            "description": "nisi incididunt",
            "dataSetId": 1616183313071840,
            "metadata": {},
            "source": "fugiat ",
            "labels": [
                {
                    "externalId": "eu tempor aliqua ipsum quis"
                },
                {
                    "externalId": "laborum est aliquip magna"
                }
            ]
        },
        {
            "name": "in culpa ullamco fugiat p",
            "externalId": "qui quis anim ",
            "parentId": 4737890210897342,
            "parentExternalId": "in",
            "description": "id Excepteur volu",
            "dataSetId": 6713921295205546,
            "metadata": {},
            "source": "nostrud",
            "labels": [
                {
                    "externalId": "dolor consequat fugiat nisi"
                },
                {
                    "externalId": "dol"
                }
            ]
        }
    ]
}'
https://domain.com/api/v1/projects/{{project}}/assets

Postman Collection Item JSON

{
  "id": "createAssets",
  "name": "Create assets",
  "request": {
    "url": {
      "host": "{{baseUrl}}",
      "path": [
        "api",
        "v1",
        "projects",
        "{{project}}",
        "assets"
      ],
      "query": [
      ],
      "variable": [
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "api-key",
        "value": "{{api-key}}",
        "description": "An admin can create API keys in the Cognite console."
      },
      {
        "key": "content-type",
        "value": "application/json"
      }
    ],
    "description": "You can create a maximum of 1000 assets per request.",
    "body": {
      "mode": "raw",
      "raw": "{\n    \"items\": [\n        {\n            \"name\": \"anim aliquip cupidatat commodo laborum\",\n            \"externalId\": \"veniam in reprehenderit culpa\",\n            \"parentId\": 7287302321534466,\n            \"parentExternalId\": \"nostrud pariatur aute\",\n            \"description\": \"nisi incididunt\",\n            \"dataSetId\": 1616183313071840,\n            \"metadata\": {},\n            \"source\": \"fugiat \",\n            \"labels\": [\n                {\n                    \"externalId\": \"eu tempor aliqua ipsum quis\"\n                },\n                {\n                    \"externalId\": \"laborum est aliquip magna\"\n                }\n            ]\n        },\n        {\n            \"name\": \"in culpa ullamco fugiat p\",\n            \"externalId\": \"qui quis anim \",\n            \"parentId\": 4737890210897342,\n            \"parentExternalId\": \"in\",\n            \"description\": \"id Excepteur volu\",\n            \"dataSetId\": 6713921295205546,\n            \"metadata\": {},\n            \"source\": \"nostrud\",\n            \"labels\": [\n                {\n                    \"externalId\": \"dolor consequat fugiat nisi\"\n                },\n                {\n                    \"externalId\": \"dol\"\n                }\n            ]\n        }\n    ]\n}"
    }
  }
}