Chilkat Online Tools

unicodeC / easybill REST API / Create attachment

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkHttpRequestW req;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    const wchar_t *created_at;
    const wchar_t *customer_id;
    const wchar_t *document_id;
    const wchar_t *file_name;
    const wchar_t *id;
    const wchar_t *project_id;
    const wchar_t *size;

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

    http = CkHttpW_Create();

    req = CkHttpRequestW_Create();
    CkHttpRequestW_putHttpVerb(req,L"POST");
    CkHttpRequestW_putPath(req,L"/rest/v1/attachments");
    CkHttpRequestW_putContentType(req,L"multipart/form-data");
    CkHttpRequestW_AddParam(req,L"file",L"<string>");

    CkHttpRequestW_AddHeader(req,L"Authorization",L"{{apiKey}}");
    CkHttpRequestW_AddHeader(req,L"Accept",L"application/json");

    resp = CkHttpW_SynchronousRequest(http,L"api.easybill.de",443,TRUE,req);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        return;
    }

    sbResponseBody = CkStringBuilderW_Create();
    CkHttpResponseW_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_LoadSb(jResp,sbResponseBody);
    CkJsonObjectW_putEmitCompact(jResp,FALSE);

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    respStatusCode = CkHttpResponseW_getStatusCode(resp);
    wprintf(L"Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkHttpResponseW_header(resp));
        wprintf(L"Failed.\n");
        CkHttpResponseW_Dispose(resp);
        CkHttpW_Dispose(http);
        CkHttpRequestW_Dispose(req);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "created_at": "<date>",
    //   "customer_id": null,
    //   "document_id": null,
    //   "file_name": "<string>",
    //   "id": "<long>",
    //   "project_id": null,
    //   "size": "<integer>"
    // }

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    created_at = CkJsonObjectW_stringOf(jResp,L"created_at");
    customer_id = CkJsonObjectW_stringOf(jResp,L"customer_id");
    document_id = CkJsonObjectW_stringOf(jResp,L"document_id");
    file_name = CkJsonObjectW_stringOf(jResp,L"file_name");
    id = CkJsonObjectW_stringOf(jResp,L"id");
    project_id = CkJsonObjectW_stringOf(jResp,L"project_id");
    size = CkJsonObjectW_stringOf(jResp,L"size");


    CkHttpW_Dispose(http);
    CkHttpRequestW_Dispose(req);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "Authorization: {{apiKey}}"
	-H "Content-Type: multipart/form-data"
	-H "Accept: application/json"
	--form 'file=<string>'
https://api.easybill.de/rest/v1/attachments

Postman Collection Item JSON

{
  "name": "Create attachment",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "multipart/form-data"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "file",
          "value": "<string>",
          "description": "(Required) ",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{baseUrl}}/attachments",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "attachments"
      ]
    }
  },
  "response": [
    {
      "name": "Successful operation",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "multipart/form-data"
          },
          {
            "key": "Accept",
            "value": "application/json"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "Authorization",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "value": "<string>",
              "description": "(Required) ",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{baseUrl}}/attachments",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "attachments"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n  \"created_at\": \"<date>\",\n  \"customer_id\": null,\n  \"document_id\": null,\n  \"file_name\": \"<string>\",\n  \"id\": \"<long>\",\n  \"project_id\": null,\n  \"size\": \"<integer>\"\n}"
    },
    {
      "name": "Too Many Requests",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "multipart/form-data"
          },
          {
            "description": "Added as a part of security scheme: apikey",
            "key": "Authorization",
            "value": "<API Key>"
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "file",
              "value": "<string>",
              "description": "(Required) ",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{baseUrl}}/attachments",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "attachments"
          ]
        }
      },
      "status": "Too Many Requests",
      "code": 429,
      "_postman_previewlanguage": "text",
      "header": [
      ],
      "cookie": [
      ],
      "body": ""
    }
  ]
}