C++
C++
Lightspeed - Create a Product
See more Lightspeed Examples
Create a new product based on the given parameters.Chilkat C++ Downloads
#include <CkHttp.h>
#include <CkJsonObject.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttp http;
// Implements the following CURL command:
// curl -u API_KEY:API_SECRET \
// -H "Content-Type: application/json" \
// -X POST \
// -d '{
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }' \
// "https://api.webshopapp.com/en/products.json"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
http.put_Login("API_KEY");
http.put_Password("API_SECRET");
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "product": {
// "visibility": "visible",
// "data01": "",
// "data02": "",
// "data03": "",
// "title": "Lookin Sharp T-Shirt",
// "fulltitle": "Lookin Sharp T-Shirt",
// "description": "Description of the Lookin Sharp T-Shirt",
// "content": "Long Description of the Lookin Sharp T-Shirt",
// "deliverydate": 6488,
// "supplier": 78794,
// "brand": 1171202
// }
// }
CkJsonObject json;
json.UpdateString("product.visibility","visible");
json.UpdateString("product.data01","");
json.UpdateString("product.data02","");
json.UpdateString("product.data03","");
json.UpdateString("product.title","Lookin Sharp T-Shirt");
json.UpdateString("product.fulltitle","Lookin Sharp T-Shirt");
json.UpdateString("product.description","Description of the Lookin Sharp T-Shirt");
json.UpdateString("product.content","Long Description of the Lookin Sharp T-Shirt");
json.UpdateInt("product.deliverydate",6488);
json.UpdateInt("product.supplier",78794);
json.UpdateInt("product.brand",1171202);
http.SetRequestHeader("Content-Type","application/json");
// Use the correct cluster for your shop. Here are the choices:
// eu1 https://api.webshopapp.com/en/
// us1 https://api.shoplightspeed.com/en/
CkHttpResponse resp;
success = http.HttpJson("POST","https://api.webshopapp.com/en/products.json",json,"application/json",resp);
if (success == false) {
std::cout << http.lastErrorText() << "\r\n";
return;
}
CkStringBuilder sbResponseBody;
resp.GetBodySb(sbResponseBody);
CkJsonObject jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
std::cout << "Response Body:" << "\r\n";
std::cout << jResp.emit() << "\r\n";
int respStatusCode = resp.get_StatusCode();
std::cout << "Response Status Code = " << respStatusCode << "\r\n";
if (respStatusCode >= 400) {
std::cout << "Response Header:" << "\r\n";
std::cout << resp.header() << "\r\n";
std::cout << "Failed." << "\r\n";
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "product": {
// "id": 20974460,
// "createdAt": "2019-05-28T20:14:26+00:00",
// "updatedAt": "2019-05-28T20:14:26+00:00",
// "isVisible": true,
// "visibility": "visible",
// "hasMatrix": false,
// "data01": "",
// "data02": "",
// "data03": "",
// "url": "lookin-sharp-t-shirt",
// "title": "Lookin' Sharp T-Shirt",
// "fulltitle": "Lookin' Sharp T-Shirt",
// "description": "Description of the Lookin' Sharp T-Shirt",
// "content": "<p>Long Description of the Lookin' Sharp T-Shirt</p>",
// "set": false,
// "brand": {
// "resource": {
// "id": 1171202,
// "url": "brands/1171202",
// "link": "https://api.shoplightspeed.com/us/brands/1171202.json"
// }
// },
// "categories": {
// "resource": {
// "id": false,
// "url": "categories/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/categories/products.json?product=20974460"
// }
// },
// "deliverydate": {
// "resource": {
// "id": 6488,
// "url": "deliverydates/6488",
// "link": "https://api.shoplightspeed.com/us/deliverydates/6488.json"
// }
// },
// "image": false,
// "images": false,
// "relations": {
// "resource": {
// "id": false,
// "url": "products/20974460/relations",
// "link": "https://api.shoplightspeed.com/us/products/20974460/relations.json"
// }
// },
// "metafields": {
// "resource": {
// "id": false,
// "url": "products/20974460/metafields",
// "link": "https://api.shoplightspeed.com/us/products/20974460/metafields.json"
// }
// },
// "reviews": {
// "resource": {
// "id": false,
// "url": "reviews?product=20974460",
// "link": "https://api.shoplightspeed.com/us/reviews.json?product=20974460"
// }
// },
// "type": false,
// "attributes": {
// "resource": {
// "id": false,
// "url": "products/20974460/attributes",
// "link": "https://api.shoplightspeed.com/us/products/20974460/attributes.json"
// }
// },
// "supplier": {
// "resource": {
// "id": 78794,
// "url": "suppliers/78794",
// "link": "https://api.shoplightspeed.com/us/suppliers/78794.json"
// }
// },
// "tags": {
// "resource": {
// "id": false,
// "url": "tags/products?product=20974460",
// "link": "https://api.shoplightspeed.com/us/tags/products.json?product=20974460"
// }
// },
// "variants": {
// "resource": {
// "id": false,
// "url": "variants?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants.json?product=20974460"
// }
// },
// "movements": {
// "resource": {
// "id": false,
// "url": "variants/movements?product=20974460",
// "link": "https://api.shoplightspeed.com/us/variants/movements.json?product=20974460"
// }
// }
// }
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
int productId = jResp.IntOf("product.id");
const char *productCreatedAt = jResp.stringOf("product.createdAt");
const char *productUpdatedAt = jResp.stringOf("product.updatedAt");
bool productIsVisible = jResp.BoolOf("product.isVisible");
const char *productVisibility = jResp.stringOf("product.visibility");
bool productHasMatrix = jResp.BoolOf("product.hasMatrix");
const char *productData01 = jResp.stringOf("product.data01");
const char *productData02 = jResp.stringOf("product.data02");
const char *productData03 = jResp.stringOf("product.data03");
const char *productUrl = jResp.stringOf("product.url");
const char *productTitle = jResp.stringOf("product.title");
const char *productFulltitle = jResp.stringOf("product.fulltitle");
const char *productDescription = jResp.stringOf("product.description");
const char *productContent = jResp.stringOf("product.content");
bool productSet = jResp.BoolOf("product.set");
int productBrandResourceId = jResp.IntOf("product.brand.resource.id");
const char *productBrandResourceUrl = jResp.stringOf("product.brand.resource.url");
const char *productBrandResourceLink = jResp.stringOf("product.brand.resource.link");
bool productCategoriesResourceId = jResp.BoolOf("product.categories.resource.id");
const char *productCategoriesResourceUrl = jResp.stringOf("product.categories.resource.url");
const char *productCategoriesResourceLink = jResp.stringOf("product.categories.resource.link");
int productDeliverydateResourceId = jResp.IntOf("product.deliverydate.resource.id");
const char *productDeliverydateResourceUrl = jResp.stringOf("product.deliverydate.resource.url");
const char *productDeliverydateResourceLink = jResp.stringOf("product.deliverydate.resource.link");
bool productImage = jResp.BoolOf("product.image");
bool productImages = jResp.BoolOf("product.images");
bool productRelationsResourceId = jResp.BoolOf("product.relations.resource.id");
const char *productRelationsResourceUrl = jResp.stringOf("product.relations.resource.url");
const char *productRelationsResourceLink = jResp.stringOf("product.relations.resource.link");
bool productMetafieldsResourceId = jResp.BoolOf("product.metafields.resource.id");
const char *productMetafieldsResourceUrl = jResp.stringOf("product.metafields.resource.url");
const char *productMetafieldsResourceLink = jResp.stringOf("product.metafields.resource.link");
bool productReviewsResourceId = jResp.BoolOf("product.reviews.resource.id");
const char *productReviewsResourceUrl = jResp.stringOf("product.reviews.resource.url");
const char *productReviewsResourceLink = jResp.stringOf("product.reviews.resource.link");
bool productType = jResp.BoolOf("product.type");
bool productAttributesResourceId = jResp.BoolOf("product.attributes.resource.id");
const char *productAttributesResourceUrl = jResp.stringOf("product.attributes.resource.url");
const char *productAttributesResourceLink = jResp.stringOf("product.attributes.resource.link");
int productSupplierResourceId = jResp.IntOf("product.supplier.resource.id");
const char *productSupplierResourceUrl = jResp.stringOf("product.supplier.resource.url");
const char *productSupplierResourceLink = jResp.stringOf("product.supplier.resource.link");
bool productTagsResourceId = jResp.BoolOf("product.tags.resource.id");
const char *productTagsResourceUrl = jResp.stringOf("product.tags.resource.url");
const char *productTagsResourceLink = jResp.stringOf("product.tags.resource.link");
bool productVariantsResourceId = jResp.BoolOf("product.variants.resource.id");
const char *productVariantsResourceUrl = jResp.stringOf("product.variants.resource.url");
const char *productVariantsResourceLink = jResp.stringOf("product.variants.resource.link");
bool productMovementsResourceId = jResp.BoolOf("product.movements.resource.id");
const char *productMovementsResourceUrl = jResp.stringOf("product.movements.resource.url");
const char *productMovementsResourceLink = jResp.stringOf("product.movements.resource.link");
}