Sample code for 30+ languages & platforms
Unicode C

Akeneo: Create New Product

See more HTTP Misc Examples

Demonstrates how to create a new product.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkJsonObjectW json;
    const wchar_t *url;
    HCkHttpResponseW resp;
    const wchar_t *location;

    success = FALSE;

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

    http = CkHttpW_Create();

    // Use your previously obtained access token.
    // See Get Akeneo Access Token
    CkHttpW_putAuthToken(http,L"access_token");

    // Build the following JSON to be sent in the request body:
    // Use this online tool to generate the code from sample JSON: 
    // Generate Code to Create JSON

    // {
    //   "identifier": "top",
    //   "enabled": true,
    //   "family": "tshirt",
    //   "categories": [
    //     "summer_collection"
    //   ],
    //   "groups": [],
    //   "parent": null,
    //   "values": {
    //     "name": [
    //       {
    //         "data": "Top",
    //         "locale": "en_US",
    //         "scope": null
    //       },
    //       {
    //         "data": "D�bardeur",
    //         "locale": "fr_FR",
    //         "scope": null
    //       }
    //     ],
    //     "description": [
    //       {
    //         "data": "Summer top",
    //         "locale": "en_US",
    //         "scope": "ecommerce"
    //       },
    //       {
    //         "data": "Top",
    //         "locale": "en_US",
    //         "scope": "tablet"
    //       },
    //       {
    //         "data": "D�bardeur pour l'�t�",
    //         "locale": "fr_FR",
    //         "scope": "ecommerce"
    //       },
    //       {
    //         "data": "D�bardeur",
    //         "locale": "fr_FR",
    //         "scope": "tablet"
    //       }
    //     ],
    //     "price": [
    //       {
    //         "locale": null,
    //         "scope": null,
    //         "data": [
    //           {
    //             "amount": "150.5",
    //             "currency": "EUR"
    //           },
    //           {
    //             "amount": "150",
    //             "currency": "USD"
    //           }
    //         ]
    //       }
    //     ],
    //     "color": [
    //       {
    //         "locale": null,
    //         "scope": null,
    //         "data": "black"
    //       }
    //     ],
    //     "size": [
    //       {
    //         "locale": null,
    //         "scope": null,
    //         "data": "m"
    //       }
    //     ]
    //   },
    //   "associations": {
    //     "PACK": {
    //       "products": [
    //         "sunglass"
    //       ],
    //       "groups": []
    //     }
    //   }
    // }
    // 

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"identifier",L"top");
    CkJsonObjectW_UpdateBool(json,L"enabled",TRUE);
    CkJsonObjectW_UpdateString(json,L"family",L"tshirt");
    CkJsonObjectW_UpdateString(json,L"categories[0]",L"summer_collection");
    CkJsonObjectW_UpdateNewArray(json,L"groups");
    CkJsonObjectW_UpdateNull(json,L"parent");
    CkJsonObjectW_UpdateString(json,L"values.name[0].data",L"Top");
    CkJsonObjectW_UpdateString(json,L"values.name[0].locale",L"en_US");
    CkJsonObjectW_UpdateNull(json,L"values.name[0].scope");
    CkJsonObjectW_UpdateString(json,L"values.name[1].data",L"D�bardeur");
    CkJsonObjectW_UpdateString(json,L"values.name[1].locale",L"fr_FR");
    CkJsonObjectW_UpdateNull(json,L"values.name[1].scope");
    CkJsonObjectW_UpdateString(json,L"values.description[0].data",L"Summer top");
    CkJsonObjectW_UpdateString(json,L"values.description[0].locale",L"en_US");
    CkJsonObjectW_UpdateString(json,L"values.description[0].scope",L"ecommerce");
    CkJsonObjectW_UpdateString(json,L"values.description[1].data",L"Top");
    CkJsonObjectW_UpdateString(json,L"values.description[1].locale",L"en_US");
    CkJsonObjectW_UpdateString(json,L"values.description[1].scope",L"tablet");
    CkJsonObjectW_UpdateString(json,L"values.description[2].data",L"D�bardeur pour l'�t�");
    CkJsonObjectW_UpdateString(json,L"values.description[2].locale",L"fr_FR");
    CkJsonObjectW_UpdateString(json,L"values.description[2].scope",L"ecommerce");
    CkJsonObjectW_UpdateString(json,L"values.description[3].data",L"D�bardeur");
    CkJsonObjectW_UpdateString(json,L"values.description[3].locale",L"fr_FR");
    CkJsonObjectW_UpdateString(json,L"values.description[3].scope",L"tablet");
    CkJsonObjectW_UpdateNull(json,L"values.price[0].locale");
    CkJsonObjectW_UpdateNull(json,L"values.price[0].scope");
    CkJsonObjectW_UpdateString(json,L"values.price[0].data[0].amount",L"150.5");
    CkJsonObjectW_UpdateString(json,L"values.price[0].data[0].currency",L"EUR");
    CkJsonObjectW_UpdateString(json,L"values.price[0].data[1].amount",L"150");
    CkJsonObjectW_UpdateString(json,L"values.price[0].data[1].currency",L"USD");
    CkJsonObjectW_UpdateNull(json,L"values.color[0].locale");
    CkJsonObjectW_UpdateNull(json,L"values.color[0].scope");
    CkJsonObjectW_UpdateString(json,L"values.color[0].data",L"black");
    CkJsonObjectW_UpdateNull(json,L"values.size[0].locale");
    CkJsonObjectW_UpdateNull(json,L"values.size[0].scope");
    CkJsonObjectW_UpdateString(json,L"values.size[0].data",L"m");
    CkJsonObjectW_UpdateString(json,L"associations.PACK.products[0]",L"sunglass");
    CkJsonObjectW_UpdateNewArray(json,L"associations.PACK.groups");

    CkJsonObjectW_putEmitCompact(json,FALSE);
    // Show the JSON to be sent..
    wprintf(L"%s\n",CkJsonObjectW_emit(json));

    url = L"http://pim.my-akeneo-site.com/api/rest/v1/products";
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpJson(http,L"POST",url,json,L"application/json",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkHttpW_Dispose(http);
        CkJsonObjectW_Dispose(json);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    wprintf(L"Response Status Code: %d\n",CkHttpResponseW_getStatusCode(resp));
    wprintf(L"Response Body: \n");
    wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));

    // The akeneo response will include a "Location" header, such as the following:
    // HTTP/1.1 201 Created
    // Date: Tue, 22 Jan 2019 10:36:35 GMT
    // Server: Apache/2
    // X-Powered-By: PHP/7.1.25
    // Cache-Control: max-age=0, private, must-revalidate, no-cache, private
    // Set-Cookie: abcdefg; path=/; HttpOnly
    // Location: http://xyz.example.com/api/rest/v1/products/L0000123
    // Vary: User-Agent
    // Content-Length: 0
    // Keep-Alive: timeout=2, max=100
    // Connection: Keep-Alive
    // Content-Type: application/json

    // Get the location header using resp.GetHeaderField
    location = CkHttpResponseW_getHeaderField(resp,L"Location");
    wprintf(L"Location: %s\n",location);


    CkHttpW_Dispose(http);
    CkJsonObjectW_Dispose(json);
    CkHttpResponseW_Dispose(resp);

    }