Chilkat Online Tools

C / New FreshBooks / Add Business

Back to Collection Items

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

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    HCkDtObj Date_format;
    int id;
    int group_id;
    const char *role;
    int identity_id;
    const char *first_name;
    const char *last_name;
    const char *email;
    const char *company;
    int business_id;
    BOOL unacknowledged_change;
    BOOL active;
    int Id;
    const char *Name;
    const char *Account_id;
    int Business_groupId;
    const char *Category;
    int AddressId;
    const char *Street;
    const char *City;
    const char *Province;
    const char *Country;
    const char *Postal_code;
    const char *Phone_number;
    int i;
    int count_i;

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

    http = CkHttp_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.

    // {
    //   "name": "Kevin's Corral",
    //   "date_format": "mm/dd/yyyy",
    //   "address_attributes": {
    //     "country": "Canada"
    //   },
    //   "phone_number_attributes": null
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"name","Kevin's Corral");
    CkJsonObject_UpdateString(json,"date_format","mm/dd/yyyy");
    CkJsonObject_UpdateString(json,"address_attributes.country","Canada");
    CkJsonObject_UpdateNull(json,"phone_number_attributes");

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

    resp = CkHttp_PostJson3(http,"https://api.freshbooks.com/auth/api/v1/users/business","application/json",json);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        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);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

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

    // {
    //   "response": {
    //     "id": 1986590,
    //     "name": "Kevin's Corral",
    //     "account_id": "lwy0vG",
    //     "business_group": {
    //       "id": 4977602,
    //       "category": "business",
    //       "members": [
    //         {
    //           "id": 6487912,
    //           "group_id": 4977602,
    //           "role": "owner",
    //           "identity_id": 37256,
    //           "first_name": "Marshall",
    //           "last_name": "Johnston",
    //           "email": "api.freshbooks@gmail.com",
    //           "company": "Kevin's Corral",
    //           "business_id": 1986590,
    //           "unacknowledged_change": false,
    //           "active": true
    //         }
    //       ]
    //     },
    //     "date_format": "mm/dd/yyyy",
    //     "address": {
    //       "id": 2448988,
    //       "street": null,
    //       "city": null,
    //       "province": null,
    //       "country": "Canada",
    //       "postal_code": null
    //     },
    //     "phone_number": null,
    //     "business_clients": [
    //     ]
    //   }
    // }

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

    Date_format = CkDtObj_Create();

    Id = CkJsonObject_IntOf(jResp,"response.id");
    Name = CkJsonObject_stringOf(jResp,"response.name");
    Account_id = CkJsonObject_stringOf(jResp,"response.account_id");
    Business_groupId = CkJsonObject_IntOf(jResp,"response.business_group.id");
    Category = CkJsonObject_stringOf(jResp,"response.business_group.category");
    CkJsonObject_DtOf(jResp,"response.date_format",FALSE,Date_format);
    AddressId = CkJsonObject_IntOf(jResp,"response.address.id");
    Street = CkJsonObject_stringOf(jResp,"response.address.street");
    City = CkJsonObject_stringOf(jResp,"response.address.city");
    Province = CkJsonObject_stringOf(jResp,"response.address.province");
    Country = CkJsonObject_stringOf(jResp,"response.address.country");
    Postal_code = CkJsonObject_stringOf(jResp,"response.address.postal_code");
    Phone_number = CkJsonObject_stringOf(jResp,"response.phone_number");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"response.business_group.members");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        id = CkJsonObject_IntOf(jResp,"response.business_group.members[i].id");
        group_id = CkJsonObject_IntOf(jResp,"response.business_group.members[i].group_id");
        role = CkJsonObject_stringOf(jResp,"response.business_group.members[i].role");
        identity_id = CkJsonObject_IntOf(jResp,"response.business_group.members[i].identity_id");
        first_name = CkJsonObject_stringOf(jResp,"response.business_group.members[i].first_name");
        last_name = CkJsonObject_stringOf(jResp,"response.business_group.members[i].last_name");
        email = CkJsonObject_stringOf(jResp,"response.business_group.members[i].email");
        company = CkJsonObject_stringOf(jResp,"response.business_group.members[i].company");
        business_id = CkJsonObject_IntOf(jResp,"response.business_group.members[i].business_id");
        unacknowledged_change = CkJsonObject_BoolOf(jResp,"response.business_group.members[i].unacknowledged_change");
        active = CkJsonObject_BoolOf(jResp,"response.business_group.members[i].active");
        i = i + 1;
    }

    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"response.business_clients");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);
    CkDtObj_Dispose(Date_format);

    }

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Content-Type: application/json"
	-d '{
    "name": "Kevin\'s Corral",
    "date_format": "mm/dd/yyyy",
    "address_attributes": {
        "country": "Canada"
    },
    "phone_number_attributes": null
}'
https://api.freshbooks.com/auth/api/v1/users/business

Postman Collection Item JSON

{
  "name": "Add Business",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"Kevin's Corral\",\n    \"date_format\": \"mm/dd/yyyy\",\n    \"address_attributes\": {\n        \"country\": \"Canada\"\n    },\n    \"phone_number_attributes\": null\n}"
    },
    "url": {
      "raw": "https://api.freshbooks.com/auth/api/v1/users/business",
      "protocol": "https",
      "host": [
        "api",
        "freshbooks",
        "com"
      ],
      "path": [
        "auth",
        "api",
        "v1",
        "users",
        "business"
      ]
    }
  },
  "response": [
    {
      "name": "Add Business",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"Kevin's Corral\",\n    \"date_format\": \"mm/dd/yyyy\",\n    \"address_attributes\": {\n        \"country\": \"Canada\"\n    },\n    \"phone_number_attributes\": null\n}"
        },
        "url": {
          "raw": "https://my.freshbooks.com/service/api/auth/api/v1/users/business",
          "protocol": "https",
          "host": [
            "my",
            "freshbooks",
            "com"
          ],
          "path": [
            "service",
            "api",
            "auth",
            "api",
            "v1",
            "users",
            "business"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Server",
          "value": "nginx"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1; mode=block"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        },
        {
          "key": "X-Permitted-Cross-Domain-Policies",
          "value": "none"
        },
        {
          "key": "Referrer-Policy",
          "value": "strict-origin-when-cross-origin"
        },
        {
          "key": "ETag",
          "value": "W/\"ece9403d440d4badd777fca953dd9580\""
        },
        {
          "key": "Cache-Control",
          "value": "no-cache"
        },
        {
          "key": "X-Request-Id",
          "value": "49e5ba95-b7dc-42c7-b0c3-8d61d99d1f39"
        },
        {
          "key": "X-Runtime",
          "value": "0.670145"
        },
        {
          "key": "X-NewRelic-App-Data",
          "value": "PxQBWV5TCBABVlVSBAUHUFQTGhEhCQ0WQg1UDl1KG39aAV0NVBZSEAEZZQAFCBdyUxAIDVl1XhUUUEJQClxZMF8XTAtWXw4IYQpMFVQyVhZLVVYNCkEUAx5UTVIZBwBXVgUEBFNVWlAIUApQDxQZAx9HDlIAVQpQBQBUV1AFXFUBXUM/"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "*"
        },
        {
          "key": "Expires",
          "value": "Wed, 25 Apr 2018 16:51:20 GMT"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Via",
          "value": "1.1 google"
        },
        {
          "key": "Via",
          "value": "1.1 varnish"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Accept-Ranges",
          "value": "bytes"
        },
        {
          "key": "Date",
          "value": "Thu, 25 Apr 2019 16:51:20 GMT"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "X-Served-By",
          "value": "cache-mdw17366-MDW"
        },
        {
          "key": "X-Cache",
          "value": "MISS"
        },
        {
          "key": "X-Cache-Hits",
          "value": "0"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Country",
          "value": "CA"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains; preload"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"response\": {\n        \"id\": 1986590,\n        \"name\": \"Kevin's Corral\",\n        \"account_id\": \"lwy0vG\",\n        \"business_group\": {\n            \"id\": 4977602,\n            \"category\": \"business\",\n            \"members\": [\n                {\n                    \"id\": 6487912,\n                    \"group_id\": 4977602,\n                    \"role\": \"owner\",\n                    \"identity_id\": 37256,\n                    \"first_name\": \"Marshall\",\n                    \"last_name\": \"Johnston\",\n                    \"email\": \"api.freshbooks@gmail.com\",\n                    \"company\": \"Kevin's Corral\",\n                    \"business_id\": 1986590,\n                    \"unacknowledged_change\": false,\n                    \"active\": true\n                }\n            ]\n        },\n        \"date_format\": \"mm/dd/yyyy\",\n        \"address\": {\n            \"id\": 2448988,\n            \"street\": null,\n            \"city\": null,\n            \"province\": null,\n            \"country\": \"Canada\",\n            \"postal_code\": null\n        },\n        \"phone_number\": null,\n        \"business_clients\": []\n    }\n}"
    }
  ]
}