Chilkat Online Tools

SQL Server / Cognite API v1 / Upload file

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

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

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

    -- {
    --   "name": "officia nisi",
    --   "externalId": "in",
    --   "directory": "et dolor",
    --   "source": "pariatur dolor aliqua nostrud",
    --   "mimeType": "occaecat est",
    --   "metadata": {},
    --   "assetIds": [
    --     4034774287403938,
    --     2425651827249892
    --   ],
    --   "dataSetId": 53722416444006,
    --   "sourceCreatedTime": 77683565,
    --   "sourceModifiedTime": 68614962,
    --   "securityCategories": [
    --     1979707127020012,
    --     747443067645264
    --   ],
    --   "labels": [
    --     {
    --       "externalId": "officia reprehenderit Ut"
    --     },
    --     {
    --       "externalId": "consectetur exercitation ipsum"
    --     }
    --   ],
    --   "geoLocation": {
    --     "type": "Feature",
    --     "geometry": {
    --       "type": "MultiPoint",
    --       "coordinates": [
    --       ]
    --     },
    --     "properties": {}
    --   }
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'officia nisi'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'externalId', 'in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'directory', 'et dolor'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'source', 'pariatur dolor aliqua nostrud'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'mimeType', 'occaecat est'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'metadata'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'assetIds[0]', 4034774287403938
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'assetIds[1]', 2425651827249892
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'dataSetId', 123
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'sourceCreatedTime', 77683565
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'sourceModifiedTime', 68614962
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'securityCategories[0]', 1979707127020012
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'securityCategories[1]', 747443067645264
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[0].externalId', 'officia reprehenderit Ut'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'labels[1].externalId', 'consectetur exercitation ipsum'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'geoLocation.type', 'Feature'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'geoLocation.geometry.type', 'MultiPoint'
    EXEC sp_OAMethod @json, 'UpdateNewArray', @success OUT, 'geoLocation.geometry.coordinates'
    EXEC sp_OAMethod @json, 'UpdateNewObject', @success OUT, 'geoLocation.properties'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'api-key', '{{api-key}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/api/v1/projects/{{project}}/files?overwrite=false', 'application/json', @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

    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 @json


END
GO

Curl Command

curl -X POST
	-H "api-key: {{api-key}}"
	-H "content-type: application/json"
	-d '{
    "name": "officia nisi",
    "externalId": "in",
    "directory": "et dolor",
    "source": "pariatur dolor aliqua nostrud",
    "mimeType": "occaecat est",
    "metadata": {},
    "assetIds": [
        4034774287403938,
        2425651827249892
    ],
    "dataSetId": 53722416444006,
    "sourceCreatedTime": 77683565,
    "sourceModifiedTime": 68614962,
    "securityCategories": [
        1979707127020012,
        747443067645264
    ],
    "labels": [
        {
            "externalId": "officia reprehenderit Ut"
        },
        {
            "externalId": "consectetur exercitation ipsum"
        }
    ],
    "geoLocation": {
        "type": "Feature",
        "geometry": {
            "type": "MultiPoint",
            "coordinates": []
        },
        "properties": {}
    }
}'
https://domain.com/api/v1/projects/{{project}}/files?overwrite=false

Postman Collection Item JSON

{
  "id": "initFileUpload",
  "name": "Upload file",
  "request": {
    "url": {
      "host": "{{baseUrl}}",
      "path": [
        "api",
        "v1",
        "projects",
        "{{project}}",
        "files"
      ],
      "query": [
        {
          "key": "overwrite",
          "description": "If 'overwrite' is set to true, and the POST body content specifies a 'externalId' field, fields for the file found for externalId can be overwritten. The default setting is false. \n\nIf metadata is included in the request body, all of the original metadata will be overwritten.\nThe actual file will be overwritten after a successful upload with the uploadUrl from the response. \nIf there is no successful upload, the current file contents will be kept. \n\nFile-Asset mappings only change if explicitly stated in the assetIds field of the POST json body. \nDo not set assetIds in request body if you want to keep the current file-asset mappings.",
          "value": "false",
          "disabled": true
        }
      ],
      "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": "Create metadata information and get an upload link for a file.\n\nTo upload the file, use the uploadUrl link in the response in a separate request. \nTo upload a file, send an HTTP PUT request to the uploadUrl with the relevant 'Content-Type' and 'Content-Length' headers.\n\nIf the uploadUrl contains the string '/v1/files/gcs_proxy/', you can make a Google Cloud Storage (GCS) resumable upload request\nas documented in https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload.\n\nThe uploadUrl expires after one week. \nAny file info entry that does not have the actual file uploaded within one week will be automatically deleted.",
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"officia nisi\",\n    \"externalId\": \"in\",\n    \"directory\": \"et dolor\",\n    \"source\": \"pariatur dolor aliqua nostrud\",\n    \"mimeType\": \"occaecat est\",\n    \"metadata\": {},\n    \"assetIds\": [\n        4034774287403938,\n        2425651827249892\n    ],\n    \"dataSetId\": 53722416444006,\n    \"sourceCreatedTime\": 77683565,\n    \"sourceModifiedTime\": 68614962,\n    \"securityCategories\": [\n        1979707127020012,\n        747443067645264\n    ],\n    \"labels\": [\n        {\n            \"externalId\": \"officia reprehenderit Ut\"\n        },\n        {\n            \"externalId\": \"consectetur exercitation ipsum\"\n        }\n    ],\n    \"geoLocation\": {\n        \"type\": \"Feature\",\n        \"geometry\": {\n            \"type\": \"MultiPoint\",\n            \"coordinates\": []\n        },\n        \"properties\": {}\n    }\n}"
    }
  }
}