Chilkat Online Tools

unicodeC / New FreshBooks / Create Single Project

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttpW http;
    BOOL success;
    HCkJsonObjectW json;
    HCkHttpResponseW resp;
    HCkStringBuilderW sbResponseBody;
    HCkJsonObjectW jResp;
    int respStatusCode;
    int id;
    int identity_id;
    const wchar_t *role;
    const wchar_t *first_name;
    const wchar_t *last_name;
    const wchar_t *email;
    const wchar_t *company;
    BOOL active;
    int Id;
    const wchar_t *Title;
    const wchar_t *Description;
    const wchar_t *Due_date;
    int Client_id;
    BOOL Internal;
    const wchar_t *Budget;
    const wchar_t *Fixed_price;
    const wchar_t *Rate;
    const wchar_t *Billing_method;
    const wchar_t *Project_type;
    BOOL Active;
    BOOL Complete;
    BOOL Sample;
    const wchar_t *Created_at;
    const wchar_t *Updated_at;
    const wchar_t *Logged_duration;
    const wchar_t *Billed_amount;
    const wchar_t *Billed_status;
    const wchar_t *Retainer_id;
    int GroupId;
    const wchar_t *Pending_invitations;
    int i;
    int count_i;

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

    // {
    //   "project": {
    //     "title": "Titikaka Hiking Guide",
    //     "client_id": 123,
    //     "project_type": "fixed_price",
    //     "fixed_price": "3000",
    //     "due_date": "2019-04-18",
    //     "services": [
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       },
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       },
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       },
    //       {
    //         "id": 123,
    //         "billable": true,
    //         "vis_state": 0
    //       }
    //     ]
    //   }
    // }

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"project.title",L"Titikaka Hiking Guide");
    CkJsonObjectW_UpdateInt(json,L"project.client_id",123);
    CkJsonObjectW_UpdateString(json,L"project.project_type",L"fixed_price");
    CkJsonObjectW_UpdateString(json,L"project.fixed_price",L"3000");
    CkJsonObjectW_UpdateString(json,L"project.due_date",L"2019-04-18");
    CkJsonObjectW_UpdateInt(json,L"project.services[0].id",123);
    CkJsonObjectW_UpdateBool(json,L"project.services[0].billable",TRUE);
    CkJsonObjectW_UpdateInt(json,L"project.services[0].vis_state",0);
    CkJsonObjectW_UpdateInt(json,L"project.services[1].id",123);
    CkJsonObjectW_UpdateBool(json,L"project.services[1].billable",TRUE);
    CkJsonObjectW_UpdateInt(json,L"project.services[1].vis_state",0);
    CkJsonObjectW_UpdateInt(json,L"project.services[2].id",123);
    CkJsonObjectW_UpdateBool(json,L"project.services[2].billable",TRUE);
    CkJsonObjectW_UpdateInt(json,L"project.services[2].vis_state",0);
    CkJsonObjectW_UpdateInt(json,L"project.services[3].id",123);
    CkJsonObjectW_UpdateBool(json,L"project.services[3].billable",TRUE);
    CkJsonObjectW_UpdateInt(json,L"project.services[3].vis_state",0);

    // Adds the "Authorization: Bearer <access_token>" header.
    CkHttpW_putAuthToken(http,L"<access_token>");
    CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");

    resp = CkHttpW_PostJson3(http,L"https://api.freshbooks.com/projects/business/{{businessId}}/projects",L"application/json",json);
    if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        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);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbResponseBody);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    CkHttpResponseW_Dispose(resp);

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

    // {
    //   "project": {
    //     "id": 3018266,
    //     "title": "Titikaka Hiking Guide",
    //     "description": null,
    //     "due_date": "2019-04-18",
    //     "client_id": 31006,
    //     "internal": false,
    //     "budget": null,
    //     "fixed_price": "3000.00",
    //     "rate": null,
    //     "billing_method": null,
    //     "project_type": "fixed_price",
    //     "active": true,
    //     "complete": false,
    //     "sample": false,
    //     "created_at": "2019-05-08T20:06:55",
    //     "updated_at": "2019-05-08T20:06:55",
    //     "logged_duration": null,
    //     "services": [
    //     ],
    //     "billed_amount": "0.00",
    //     "billed_status": "unbilled",
    //     "retainer_id": null,
    //     "group": {
    //       "id": 5049390,
    //       "members": [
    //         {
    //           "id": 6578346,
    //           "identity_id": 37256,
    //           "role": "owner",
    //           "first_name": "Marshall",
    //           "last_name": "Johnston",
    //           "email": "api.freshbooks@freshbooks.com",
    //           "company": "Postman Sandbox",
    //           "active": true
    //         }
    //       ],
    //       "pending_invitations": null
    //     }
    //   }
    // }

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

    Id = CkJsonObjectW_IntOf(jResp,L"project.id");
    Title = CkJsonObjectW_stringOf(jResp,L"project.title");
    Description = CkJsonObjectW_stringOf(jResp,L"project.description");
    Due_date = CkJsonObjectW_stringOf(jResp,L"project.due_date");
    Client_id = CkJsonObjectW_IntOf(jResp,L"project.client_id");
    Internal = CkJsonObjectW_BoolOf(jResp,L"project.internal");
    Budget = CkJsonObjectW_stringOf(jResp,L"project.budget");
    Fixed_price = CkJsonObjectW_stringOf(jResp,L"project.fixed_price");
    Rate = CkJsonObjectW_stringOf(jResp,L"project.rate");
    Billing_method = CkJsonObjectW_stringOf(jResp,L"project.billing_method");
    Project_type = CkJsonObjectW_stringOf(jResp,L"project.project_type");
    Active = CkJsonObjectW_BoolOf(jResp,L"project.active");
    Complete = CkJsonObjectW_BoolOf(jResp,L"project.complete");
    Sample = CkJsonObjectW_BoolOf(jResp,L"project.sample");
    Created_at = CkJsonObjectW_stringOf(jResp,L"project.created_at");
    Updated_at = CkJsonObjectW_stringOf(jResp,L"project.updated_at");
    Logged_duration = CkJsonObjectW_stringOf(jResp,L"project.logged_duration");
    Billed_amount = CkJsonObjectW_stringOf(jResp,L"project.billed_amount");
    Billed_status = CkJsonObjectW_stringOf(jResp,L"project.billed_status");
    Retainer_id = CkJsonObjectW_stringOf(jResp,L"project.retainer_id");
    GroupId = CkJsonObjectW_IntOf(jResp,L"project.group.id");
    Pending_invitations = CkJsonObjectW_stringOf(jResp,L"project.group.pending_invitations");
    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"project.services");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"project.group.members");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        id = CkJsonObjectW_IntOf(jResp,L"project.group.members[i].id");
        identity_id = CkJsonObjectW_IntOf(jResp,L"project.group.members[i].identity_id");
        role = CkJsonObjectW_stringOf(jResp,L"project.group.members[i].role");
        first_name = CkJsonObjectW_stringOf(jResp,L"project.group.members[i].first_name");
        last_name = CkJsonObjectW_stringOf(jResp,L"project.group.members[i].last_name");
        email = CkJsonObjectW_stringOf(jResp,L"project.group.members[i].email");
        company = CkJsonObjectW_stringOf(jResp,L"project.group.members[i].company");
        active = CkJsonObjectW_BoolOf(jResp,L"project.group.members[i].active");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbResponseBody);
    CkJsonObjectW_Dispose(jResp);

    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
  "project": {
    "title": "Titikaka Hiking Guide",
    "client_id": {{customerId}},
    "project_type": "fixed_price",
    "fixed_price": "3000",
    "due_date": "2019-04-18",
    "services": 
    [
    	{"id": {{serviceid}}, "billable": true, "vis_state": 0}, {"id": {{serviceid}},"billable": true, "vis_state": 0}, {"id": {{serviceid}}, "billable": true, "vis_state": 0}, {"id": {{serviceid}}, "billable": true, "vis_state": 0}
    	]
  }
}'
https://api.freshbooks.com/projects/business/{{businessId}}/projects

Postman Collection Item JSON

{
  "name": "Create Single Project",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "let jsonData = JSON.parse(responseBody);",
          "pm.environment.set(\"projectId\", jsonData.project.id);",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"project\": {\n    \"title\": \"Titikaka Hiking Guide\",\n    \"client_id\": {{customerId}},\n    \"project_type\": \"fixed_price\",\n    \"fixed_price\": \"3000\",\n    \"due_date\": \"2019-04-18\",\n    \"services\": \n    [\n    \t{\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}},\"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}, {\"id\": {{serviceid}}, \"billable\": true, \"vis_state\": 0}\n    \t]\n  }\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "projects",
        "business",
        "{{businessId}}",
        "projects"
      ]
    }
  },
  "response": [
    {
      "name": "Create Single Project",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n  \"project\": {\n    \"title\": \"Titikaka Hiking Guide\",\n    \"client_id\": {{customerId}},\n    \"project_type\": \"fixed_price\",\n    \"fixed_price\": \"3000\",\n    \"due_date\": \"2019-04-18\"\n  }\n}"
        },
        "url": {
          "raw": "https://api.freshbooks.com/projects/business/{{businessId}}/projects",
          "protocol": "https",
          "host": [
            "api",
            "freshbooks",
            "com"
          ],
          "path": [
            "projects",
            "business",
            "{{businessId}}",
            "projects"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/vnd.api+json"
        },
        {
          "key": "X-Version",
          "value": "1905.7.0"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVllUBAQOU10TGhE1AwE2QgNWEVlbQFtcCxYnRA9QFg1ZWU4FDFZFVRYSBxlDUhULRERQBxVNEkINUgdbTEccQQBKDkQTUAdeRkUODAZbR0NGTVIbARlUVAcEB1VTVVoBUQhWAQsFBB9UDlQdQ1dSVVBSBFFRD1dVVldVAAIVSgJQWkAHOw=="
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "20"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "8"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1557346076"
        },
        {
          "key": "Retry-After",
          "value": "60"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Tue, 08 May 2018 20:06:55 GMT"
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Content-Length",
          "value": "759"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Wed, 08 May 2019 20:06: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    \"project\": {\n        \"id\": 3018266,\n        \"title\": \"Titikaka Hiking Guide\",\n        \"description\": null,\n        \"due_date\": \"2019-04-18\",\n        \"client_id\": 31006,\n        \"internal\": false,\n        \"budget\": null,\n        \"fixed_price\": \"3000.00\",\n        \"rate\": null,\n        \"billing_method\": null,\n        \"project_type\": \"fixed_price\",\n        \"active\": true,\n        \"complete\": false,\n        \"sample\": false,\n        \"created_at\": \"2019-05-08T20:06:55\",\n        \"updated_at\": \"2019-05-08T20:06:55\",\n        \"logged_duration\": null,\n        \"services\": [],\n        \"billed_amount\": \"0.00\",\n        \"billed_status\": \"unbilled\",\n        \"retainer_id\": null,\n        \"group\": {\n            \"id\": 5049390,\n            \"members\": [\n                {\n                    \"id\": 6578346,\n                    \"identity_id\": 37256,\n                    \"role\": \"owner\",\n                    \"first_name\": \"Marshall\",\n                    \"last_name\": \"Johnston\",\n                    \"email\": \"api.freshbooks@freshbooks.com\",\n                    \"company\": \"Postman Sandbox\",\n                    \"active\": true\n                }\n            ],\n            \"pending_invitations\": null\n        }\n    }\n}"
    }
  ]
}