Unicode C
Unicode C
Shopify Create a new unpublished product
See more Shopify Examples
Create a new unpublished productChilkat Unicode C Downloads
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
HCkJsonObjectW jsonReq;
HCkStringBuilderW sbReq;
HCkStringBuilderW sbJson;
HCkJsonObjectW json;
int productId;
const wchar_t *productTitle;
const wchar_t *productBody_html;
const wchar_t *productVendor;
const wchar_t *productProduct_type;
const wchar_t *productCreated_at;
const wchar_t *productHandle;
const wchar_t *productUpdated_at;
BOOL productPublished_at;
BOOL productTemplate_suffix;
const wchar_t *productPublished_scope;
const wchar_t *productTags;
BOOL productImage;
int i;
int count_i;
int id;
int product_id;
const wchar_t *title;
const wchar_t *price;
const wchar_t *sku;
int position;
int grams;
const wchar_t *inventory_policy;
BOOL compare_at_price;
const wchar_t *fulfillment_service;
BOOL inventory_management;
const wchar_t *option1;
BOOL option2;
BOOL option3;
const wchar_t *created_at;
const wchar_t *updated_at;
BOOL taxable;
BOOL barcode;
BOOL image_id;
int inventory_quantity;
int weight;
const wchar_t *weight_unit;
int old_inventory_quantity;
BOOL requires_shipping;
const wchar_t *name;
int j;
int count_j;
const wchar_t *strVal;
success = FALSE;
rest = CkRestW_Create();
CkRestW_SetAuthBasic(rest,L"SHOPIFY_PRIVATE_API_KEY",L"SHOPIFY_PRIVATE_API_KEY");
success = CkRestW_Connect(rest,L"chilkat.myshopify.com",443,TRUE,TRUE);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
return;
}
// The following code creates the JSON request body.
// The JSON created by this code is shown below.
jsonReq = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(jsonReq,L"product.title",L"Burton Custom Freestyle 151");
CkJsonObjectW_UpdateString(jsonReq,L"product.body_html",L"<strong>Good snowboard!</strong>");
CkJsonObjectW_UpdateString(jsonReq,L"product.vendor",L"Burton");
CkJsonObjectW_UpdateString(jsonReq,L"product.product_type",L"Snowboard");
CkJsonObjectW_UpdateBool(jsonReq,L"product.published",FALSE);
// The JSON request body created by the above code:
// {
// "product": {
// "title": "Burton Custom Freestyle 151",
// "body_html": "<strong>Good snowboard!<\/strong>",
// "vendor": "Burton",
// "product_type": "Snowboard",
// "published": false
// }
// }
sbReq = CkStringBuilderW_Create();
CkJsonObjectW_EmitSb(jsonReq,sbReq);
CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
sbJson = CkStringBuilderW_Create();
success = CkRestW_FullRequestSb(rest,L"POST",L"/admin/products.json ",sbReq,sbJson);
if (success != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(jsonReq);
CkStringBuilderW_Dispose(sbReq);
CkStringBuilderW_Dispose(sbJson);
return;
}
if (CkRestW_getResponseStatusCode(rest) != 201) {
wprintf(L"Received error response code: %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"Response body:\n");
wprintf(L"%s\n",CkStringBuilderW_getAsString(sbJson));
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(jsonReq);
CkStringBuilderW_Dispose(sbReq);
CkStringBuilderW_Dispose(sbJson);
return;
}
json = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(json,sbJson);
// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.
productId = CkJsonObjectW_IntOf(json,L"product.id");
productTitle = CkJsonObjectW_stringOf(json,L"product.title");
productBody_html = CkJsonObjectW_stringOf(json,L"product.body_html");
productVendor = CkJsonObjectW_stringOf(json,L"product.vendor");
productProduct_type = CkJsonObjectW_stringOf(json,L"product.product_type");
productCreated_at = CkJsonObjectW_stringOf(json,L"product.created_at");
productHandle = CkJsonObjectW_stringOf(json,L"product.handle");
productUpdated_at = CkJsonObjectW_stringOf(json,L"product.updated_at");
productPublished_at = CkJsonObjectW_IsNullOf(json,L"product.published_at");
productTemplate_suffix = CkJsonObjectW_IsNullOf(json,L"product.template_suffix");
productPublished_scope = CkJsonObjectW_stringOf(json,L"product.published_scope");
productTags = CkJsonObjectW_stringOf(json,L"product.tags");
productImage = CkJsonObjectW_IsNullOf(json,L"product.image");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(json,L"product.variants");
while (i < count_i) {
CkJsonObjectW_putI(json,i);
id = CkJsonObjectW_IntOf(json,L"product.variants[i].id");
product_id = CkJsonObjectW_IntOf(json,L"product.variants[i].product_id");
title = CkJsonObjectW_stringOf(json,L"product.variants[i].title");
price = CkJsonObjectW_stringOf(json,L"product.variants[i].price");
sku = CkJsonObjectW_stringOf(json,L"product.variants[i].sku");
position = CkJsonObjectW_IntOf(json,L"product.variants[i].position");
grams = CkJsonObjectW_IntOf(json,L"product.variants[i].grams");
inventory_policy = CkJsonObjectW_stringOf(json,L"product.variants[i].inventory_policy");
compare_at_price = CkJsonObjectW_IsNullOf(json,L"product.variants[i].compare_at_price");
fulfillment_service = CkJsonObjectW_stringOf(json,L"product.variants[i].fulfillment_service");
inventory_management = CkJsonObjectW_IsNullOf(json,L"product.variants[i].inventory_management");
option1 = CkJsonObjectW_stringOf(json,L"product.variants[i].option1");
option2 = CkJsonObjectW_IsNullOf(json,L"product.variants[i].option2");
option3 = CkJsonObjectW_IsNullOf(json,L"product.variants[i].option3");
created_at = CkJsonObjectW_stringOf(json,L"product.variants[i].created_at");
updated_at = CkJsonObjectW_stringOf(json,L"product.variants[i].updated_at");
taxable = CkJsonObjectW_BoolOf(json,L"product.variants[i].taxable");
barcode = CkJsonObjectW_IsNullOf(json,L"product.variants[i].barcode");
image_id = CkJsonObjectW_IsNullOf(json,L"product.variants[i].image_id");
inventory_quantity = CkJsonObjectW_IntOf(json,L"product.variants[i].inventory_quantity");
weight = CkJsonObjectW_IntOf(json,L"product.variants[i].weight");
weight_unit = CkJsonObjectW_stringOf(json,L"product.variants[i].weight_unit");
old_inventory_quantity = CkJsonObjectW_IntOf(json,L"product.variants[i].old_inventory_quantity");
requires_shipping = CkJsonObjectW_BoolOf(json,L"product.variants[i].requires_shipping");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(json,L"product.options");
while (i < count_i) {
CkJsonObjectW_putI(json,i);
id = CkJsonObjectW_IntOf(json,L"product.options[i].id");
product_id = CkJsonObjectW_IntOf(json,L"product.options[i].product_id");
name = CkJsonObjectW_stringOf(json,L"product.options[i].name");
position = CkJsonObjectW_IntOf(json,L"product.options[i].position");
j = 0;
count_j = CkJsonObjectW_SizeOfArray(json,L"product.options[i].values");
while (j < count_j) {
CkJsonObjectW_putJ(json,j);
strVal = CkJsonObjectW_stringOf(json,L"product.options[i].values[j]");
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(json,L"product.images");
while (i < count_i) {
CkJsonObjectW_putI(json,i);
i = i + 1;
}
// A sample JSON response body that is parsed by the above code:
// {
// "product": {
// "id": 1071559753,
// "title": "Burton Custom Freestyle 151",
// "body_html": "<strong>Good snowboard!<\/strong>",
// "vendor": "Burton",
// "product_type": "Snowboard",
// "created_at": "2017-09-22T14:48:52-04:00",
// "handle": "burton-custom-freestyle-151",
// "updated_at": "2017-09-22T14:48:52-04:00",
// "published_at": null,
// "template_suffix": null,
// "published_scope": "global",
// "tags": "",
// "variants": [
// {
// "id": 1070325224,
// "product_id": 1071559753,
// "title": "Default Title",
// "price": "0.00",
// "sku": "",
// "position": 1,
// "grams": 0,
// "inventory_policy": "deny",
// "compare_at_price": null,
// "fulfillment_service": "manual",
// "inventory_management": null,
// "option1": "Default Title",
// "option2": null,
// "option3": null,
// "created_at": "2017-09-22T14:48:52-04:00",
// "updated_at": "2017-09-22T14:48:52-04:00",
// "taxable": true,
// "barcode": null,
// "image_id": null,
// "inventory_quantity": 1,
// "weight": 0.0,
// "weight_unit": "lb",
// "old_inventory_quantity": 1,
// "requires_shipping": true
// }
// ],
// "options": [
// {
// "id": 1022828912,
// "product_id": 1071559753,
// "name": "Title",
// "position": 1,
// "values": [
// "Default Title"
// ]
// }
// ],
// "images": [
// ],
// "image": null
// }
// }
wprintf(L"Example Completed.\n");
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(jsonReq);
CkStringBuilderW_Dispose(sbReq);
CkStringBuilderW_Dispose(sbJson);
CkJsonObjectW_Dispose(json);
}