Chilkat Online Tools

C / New FreshBooks / Upload Logo

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkHttpRequest req;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *v_Jwt;
    const char *Public_id;
    const char *Filename;
    const char *link;

    // 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,"/uploads/account/{{accountId}}/images");
    CkHttpRequest_putContentType(req,"multipart/form-data");
    success = CkHttpRequest_AddFileForUpload2(req,"content"," path to file","application/octet-stream");

    CkHttpRequest_AddHeader(req,"Authorization","Bearer <access_token>");
    CkHttpRequest_AddHeader(req,"Expect","100-continue");

    resp = CkHttp_SynchronousRequest(http,"api.freshbooks.com",443,TRUE,req);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        return;
    }

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

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkHttpRequest_Dispose(req);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

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

    // {
    //   "image": {
    //     "jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE",
    //     "public_id": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE",
    //     "filename": "upload-9198160f94ca297e174a602f40899a3c9f76099d"
    //   },
    //   "link": "https://my.freshbooks.com/service/uploads/images/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE"
    // }

    // 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.

    v_Jwt = CkJsonObject_stringOf(jResp,"image.jwt");
    Public_id = CkJsonObject_stringOf(jResp,"image.public_id");
    Filename = CkJsonObject_stringOf(jResp,"image.filename");
    link = CkJsonObject_stringOf(jResp,"link");


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

    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: multipart/form-data"
	--form 'content=@"/path/to/file"'
https://api.freshbooks.com/uploads/account/{{accountId}}/images

Postman Collection Item JSON

{
  "name": "Upload Logo",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody); ",
          "pm.environment.set(\"uploadImg\", \"\\'\" + jsonData.image.jwt + \"\\\"\")"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "multipart/form-data",
        "type": "text",
        "disabled": true
      }
    ],
    "body": {
      "mode": "formdata",
      "formdata": [
        {
          "key": "content",
          "type": "file",
          "src": [
          ]
        }
      ]
    },
    "url": {
      "raw": "https://api.freshbooks.com/uploads/account/{{accountId}}/images",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "uploads",
        "account",
        "{{accountId}}",
        "images"
      ]
    }
  },
  "response": [
    {
      "name": "Upload Logo",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "multipart/form-data",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "content",
              "type": "file",
              "src": [
              ]
            }
          ]
        },
        "url": {
          "raw": "https://api.freshbooks.com/uploads/account/{{accountId}}/images",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "uploads",
            "account",
            "{{accountId}}",
            "images"
          ]
        }
      },
      "status": "CREATED",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1902.2.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllRBQIEU1UTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4TE1RcUQATTEVURAkRQ1VWEUgKD1EFXRECUVlTVABKE1QSXBcWVVJDSlMUAh5UV1AGAwNfUgYDBFdWVFECTgtSDwoMCh9HX1IJVAIGUlIHVFQHXAoJBUNOUVBbFQFs"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 24 Apr 2018 17:11:52 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "1324"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 24 Apr 2019 17:11:52 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17356-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"image\": {\n        \"jwt\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE\",\n        \"public_id\": \"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE\",\n        \"filename\": \"upload-9198160f94ca297e174a602f40899a3c9f76099d\"\n    },\n    \"link\": \"https://my.freshbooks.com/service/uploads/images/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhY2NvdW50Ijo0MjM2NDEwLCJvcmlnaW5hbF9maWxlbmFtZSI6InRvcm9udG8tc2t5bGluZS5qcGciLCJidWNrZXQiOiJ1cGxvYWRzIiwiZmlsZW5hbWUiOiJ1cGxvYWQtOTE5ODE2MGY5NGNhMjk3ZTE3NGE2MDJmNDA4OTlhM2M5Zjc2MDk5ZCIsImxlbmd0aCI6MzA3MDY4LCJrZXkiOiInZG9jcy0nLTQyMzY0MTAvdXBsb2FkLTkxOTgxNjBmOTRjYTI5N2UxNzRhNjAyZjQwODk5YTNjOWY3NjA5OWQifQ.yAfSd2cIAf2GONitgBmimMCHxvH9OYij7g0WMsiLsaE\"\n}"
    },
    {
      "name": "Upload Logo - Error",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "multipart/form-data",
            "type": "text",
            "disabled": true
          }
        ],
        "body": {
          "mode": "formdata",
          "formdata": [
            {
              "key": "content",
              "type": "file",
              "src": [
              ]
            }
          ]
        },
        "url": {
          "raw": "https://api.freshbooks.com/uploads/account/{{accountId}}/images",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "uploads",
            "account",
            "{{accountId}}",
            "images"
          ]
        }
      },
      "status": "BAD REQUEST",
      "code": 400,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1904.0.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllRBQIEU1UTGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4TE1RcUQATTEVURAkRQ1VWEUgKD1EFXRECUVlTVABKE1QSXBcWVVJDSlMUAx5XV1cGBANWVgYPAFpeV1AITghOGllXAldcWwICWAJbXA9UBVRBFFVRCBIHag=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "0"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557345773"
        },
        {
          "key": "Retry-After",
          "value": "57"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "32"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 08 May 2019 20:01:55 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17361-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"error\": \"Content is required\"\n}"
    }
  ]
}