Chilkat Online Tools

delphiDll / Zoho CRM REST APIs / Upload File

Back to Collection Items

var
http: HCkHttp;
success: Boolean;
req: HCkHttpRequest;
resp: HCkHttpResponse;
sbResponseBody: HCkStringBuilder;
jResp: HCkJsonObject;
respStatusCode: Integer;
code: PWideChar;
Created_time: PWideChar;
File_id: PWideChar;
message: PWideChar;
status: PWideChar;

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

http := CkHttp_Create();

req := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(req,'POST');
CkHttpRequest_putPath(req,'/crm/v2.1/upload');
CkHttpRequest_putContentType(req,'multipart/form-data');
success := CkHttpRequest_AddFileForUpload2(req,'file',' path to file','application/octet-stream');

CkHttpRequest_AddHeader(req,'X-CRM-ORG','694902309');
CkHttpRequest_AddHeader(req,'Expect','100-continue');
CkHttpRequest_AddHeader(req,'feature','bulk-write');
CkHttpRequest_AddHeader(req,'Authorization','Bearer <access_token>');

resp := CkHttp_SynchronousRequest(http,'content.zohoapis.com',443,True,req);
if (CkHttp_getLastMethodSuccess(http) = False) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

sbResponseBody := CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);

jResp := CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,False);

Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(CkJsonObject__emit(jResp));

respStatusCode := CkHttpResponse_getStatusCode(resp);
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(CkHttpResponse__header(resp));
    Memo1.Lines.Add('Failed.');
    CkHttpResponse_Dispose(resp);
    Exit;
  end;
CkHttpResponse_Dispose(resp);

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "code": "FILE_UPLOAD_SUCCESS",
//   "details": {
//     "created_time": "2021-05-11T12:03:18+05:30",
//     "file_id": "4150868000005353001"
//   },
//   "message": "file uploaded.",
//   "status": "success"
// }

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

code := CkJsonObject__stringOf(jResp,'code');
Created_time := CkJsonObject__stringOf(jResp,'details.created_time');
File_id := CkJsonObject__stringOf(jResp,'details.file_id');
message := CkJsonObject__stringOf(jResp,'message');
status := CkJsonObject__stringOf(jResp,'status');

CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "X-CRM-ORG: 694902309"
	-H "feature: bulk-write"
	--form 'file=@"/path/to/file"'
https://content.zohoapis.com/crm/v2.1/upload

Postman Collection Item JSON

{
  "name": "Upload File",
  "request": {
    "auth": {
      "type": "bearer",
      "bearer": [
        {
          "key": "token",
          "value": "{{access-token}}",
          "type": "string"
        }
      ]
    },
    "method": "POST",
    "header": [
      {
        "key": "X-CRM-ORG",
        "value": "694902309",
        "type": "text"
      },
      {
        "key": "feature",
        "value": "bulk-write",
        "type": "text"
      },
      {
        "key": "",
        "value": "",
        "type": "text",
        "disabled": true
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "file",
          "type": "file",
          "src": "/Users/sneha-9300/Desktop/SampleCsvFile.zip"
        }
      ]
    },
    "url": {
      "raw": "https://content.zohoapis.com/crm/v2.1/upload",
      "protocol": "https",
      "host": [
        "content",
        "zohoapis",
        "com"
      ],
      "path": [
        "crm",
        "v2.1",
        "upload"
      ]
    },
    "description": "To upload a CSV file in ZIP format for bulk write API. The response contains the **file_id**. Use this ID while making the bulk write request."
  },
  "response": [
    {
      "name": "SUCCESS RESPONSE",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "X-CRM-ORG",
            "value": "694902309",
            "type": "text"
          },
          {
            "key": "feature",
            "value": "bulk-write",
            "type": "text"
          },
          {
            "key": "",
            "value": "",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "type": "file",
              "src": "/Users/sneha-9300/Desktop/SampleCsvFile.csv.zip"
            }
          ]
        },
        "url": {
          "raw": "https://content.zohoapis.com/crm/v2.1/upload",
          "protocol": "https",
          "host": [
            "content",
            "zohoapis",
            "com"
          ],
          "path": [
            "crm",
            "v2.1",
            "upload"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 10 May 2021 23:33:18 PDT"
        },
        {
          "key": "Last-Modified",
          "value": "Mon, 10 May 2021 23:33:18 PDT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Connection",
          "value": "Keep-Alive"
        },
        {
          "key": "Server",
          "value": "AWServer"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=15768000"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Request-Served-By",
          "value": "US4"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Content-Length",
          "value": "163"
        },
        {
          "key": "Expires",
          "value": "1"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"code\": \"FILE_UPLOAD_SUCCESS\",\n    \"details\": {\n        \"created_time\": \"2021-05-11T12:03:18+05:30\",\n        \"file_id\": \"4150868000005353001\"\n    },\n    \"message\": \"file uploaded.\",\n    \"status\": \"success\"\n}"
    },
    {
      "name": "INVALID_FILE_FORMAT",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "X-CRM-ORG",
            "value": "694902309",
            "type": "text"
          },
          {
            "key": "feature",
            "value": "bulk-write",
            "type": "text"
          },
          {
            "key": "",
            "value": "",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "type": "file",
              "src": "/Users/sneha-9300/Downloads/_Bulk Write.erb"
            }
          ]
        },
        "url": {
          "raw": "https://content.zohoapis.com/crm/v2.1/upload",
          "protocol": "https",
          "host": [
            "content",
            "zohoapis",
            "com"
          ],
          "path": [
            "crm",
            "v2.1",
            "upload"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 10 May 2021 23:33:47 PDT"
        },
        {
          "key": "Last-Modified",
          "value": "Mon, 10 May 2021 23:33:47 PDT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Connection",
          "value": "Keep-Alive"
        },
        {
          "key": "Server",
          "value": "AWServer"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=15768000"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Request-Served-By",
          "value": "US4"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Content-Length",
          "value": "164"
        },
        {
          "key": "Expires",
          "value": "1"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"code\": \"INVALID_FILE_FORMAT\",\n    \"details\": {\n        \"created_time\": \"2021-05-11T12:03:47+05:30\"\n    },\n    \"message\": \"invalid file format. only zip format is supported\",\n    \"status\": \"error\"\n}"
    },
    {
      "name": "METHOD_NOT_ALLOWED",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "X-CRM-ORG",
            "value": "694902309",
            "type": "text"
          },
          {
            "key": "feature",
            "value": "bulk-write",
            "type": "text"
          },
          {
            "key": "",
            "value": "",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "type": "file",
              "src": "/Users/sneha-9300/Downloads/_Bulk Write.erb"
            }
          ]
        },
        "url": {
          "raw": "https://content.zohoapis.com/crm/v2.1/upload",
          "protocol": "https",
          "host": [
            "content",
            "zohoapis",
            "com"
          ],
          "path": [
            "crm",
            "v2.1",
            "upload"
          ]
        }
      },
      "status": "Method Not AllowedDate: Mon May 10 23:34:25 PDT 2021",
      "code": 405,
      "_postman_previewlanguage": "plain",
      "header": [
      ],
      "cookie": [
      ],
      "body": null
    },
    {
      "name": "AUTHENTICATION FAILURE",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "X-CRM-ORG",
            "value": "694902309",
            "type": "text"
          },
          {
            "key": "feature",
            "value": "bulk-write",
            "type": "text"
          },
          {
            "key": "",
            "value": "",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "type": "file",
              "src": "/Users/sneha-9300/Downloads/_Bulk Write.erb"
            }
          ]
        },
        "url": {
          "raw": "https://content.zohoapis.com/crm/v2.1/upload",
          "protocol": "https",
          "host": [
            "content",
            "zohoapis",
            "com"
          ],
          "path": [
            "crm",
            "v2.1",
            "upload"
          ]
        }
      },
      "status": "Unauthorized Error",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Mon, 10 May 2021 23:34:53 PDT"
        },
        {
          "key": "Last-Modified",
          "value": "Mon, 10 May 2021 23:34:53 PDT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Connection",
          "value": "Keep-Alive"
        },
        {
          "key": "Server",
          "value": "AWServer"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=15768000"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Request-Served-By",
          "value": "US4"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Expires",
          "value": "1"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"ERROR_MESSAGE\": \"Unauthorized Error\",\n    \"ERROR_CODE\": 401\n}"
    },
    {
      "name": "INVALID_OAUTHTOKEN",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "X-CRM-ORG",
            "value": "694902309",
            "type": "text"
          },
          {
            "key": "feature",
            "value": "bulk-write",
            "type": "text"
          },
          {
            "key": "",
            "value": "",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "type": "file",
              "src": "/Users/sneha-9300/Desktop/SampleCsvFile.zip"
            }
          ]
        },
        "url": {
          "raw": "https://content.zohoapis.com/crm/v2.1/upload",
          "protocol": "https",
          "host": [
            "content",
            "zohoapis",
            "com"
          ],
          "path": [
            "crm",
            "v2.1",
            "upload"
          ]
        }
      },
      "status": "INVALID_OAUTHTOKEN",
      "code": 401,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Thu, 13 May 2021 01:24:16 PDT"
        },
        {
          "key": "Last-Modified",
          "value": "Thu, 13 May 2021 01:24:16 PDT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Connection",
          "value": "Keep-Alive"
        },
        {
          "key": "Server",
          "value": "AWServer"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=15768000"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Request-Served-By",
          "value": "US4"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Expires",
          "value": "1"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"ERROR_MESSAGE\": \"INVALID_OAUTHTOKEN\",\n    \"ERROR_CODE\": 401\n}"
    }
  ]
}