Unicode C++
Unicode C++
Akeneo: Create New Product
See more HTTP Misc Examples
Demonstrates how to create a new product.Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
// Use your previously obtained access token.
// See Get Akeneo Access Token
http.put_AuthToken(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": []
// }
// }
// }
//
CkJsonObjectW json;
json.UpdateString(L"identifier",L"top");
json.UpdateBool(L"enabled",true);
json.UpdateString(L"family",L"tshirt");
json.UpdateString(L"categories[0]",L"summer_collection");
json.UpdateNewArray(L"groups");
json.UpdateNull(L"parent");
json.UpdateString(L"values.name[0].data",L"Top");
json.UpdateString(L"values.name[0].locale",L"en_US");
json.UpdateNull(L"values.name[0].scope");
json.UpdateString(L"values.name[1].data",L"D�bardeur");
json.UpdateString(L"values.name[1].locale",L"fr_FR");
json.UpdateNull(L"values.name[1].scope");
json.UpdateString(L"values.description[0].data",L"Summer top");
json.UpdateString(L"values.description[0].locale",L"en_US");
json.UpdateString(L"values.description[0].scope",L"ecommerce");
json.UpdateString(L"values.description[1].data",L"Top");
json.UpdateString(L"values.description[1].locale",L"en_US");
json.UpdateString(L"values.description[1].scope",L"tablet");
json.UpdateString(L"values.description[2].data",L"D�bardeur pour l'�t�");
json.UpdateString(L"values.description[2].locale",L"fr_FR");
json.UpdateString(L"values.description[2].scope",L"ecommerce");
json.UpdateString(L"values.description[3].data",L"D�bardeur");
json.UpdateString(L"values.description[3].locale",L"fr_FR");
json.UpdateString(L"values.description[3].scope",L"tablet");
json.UpdateNull(L"values.price[0].locale");
json.UpdateNull(L"values.price[0].scope");
json.UpdateString(L"values.price[0].data[0].amount",L"150.5");
json.UpdateString(L"values.price[0].data[0].currency",L"EUR");
json.UpdateString(L"values.price[0].data[1].amount",L"150");
json.UpdateString(L"values.price[0].data[1].currency",L"USD");
json.UpdateNull(L"values.color[0].locale");
json.UpdateNull(L"values.color[0].scope");
json.UpdateString(L"values.color[0].data",L"black");
json.UpdateNull(L"values.size[0].locale");
json.UpdateNull(L"values.size[0].scope");
json.UpdateString(L"values.size[0].data",L"m");
json.UpdateString(L"associations.PACK.products[0]",L"sunglass");
json.UpdateNewArray(L"associations.PACK.groups");
json.put_EmitCompact(false);
// Show the JSON to be sent..
wprintf(L"%s\n",json.emit());
const wchar_t *url = L"http://pim.my-akeneo-site.com/api/rest/v1/products";
CkHttpResponseW resp;
success = http.HttpJson(L"POST",url,json,L"application/json",resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
wprintf(L"Response Status Code: %d\n",resp.get_StatusCode());
wprintf(L"Response Body: \n");
wprintf(L"%s\n",resp.bodyStr());
// 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
const wchar_t *location = resp.getHeaderField(L"Location");
wprintf(L"Location: %s\n",location);
}