Unicode C++
Unicode C++
Lightspeed - Upload an Image
See more Lightspeed Examples
Create (uploads) a new product image.Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkBinDataW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.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.
CkHttpW http;
// Implements the following CURL command:
// curl -u API_KEY:API_SECRET \
// -H "Content-Type: application/json" \
// -X POST \
// -d '{
// "productImage": {
// "attachment": "base64-encoded-contents",
// "filename": "the-filename-with-extension-goes-here.jpg"
// }
// }' \
// "https://api.webshopapp.com/en/products/product_id/images.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(L"API_KEY");
http.put_Password(L"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.
// {
// "productImage": {
// "attachment": "base64-encoded-contents",
// "filename": "the-filename-with-extension-goes-here.jpg"
// }
// }
CkBinDataW bdImage;
success = bdImage.LoadFile(L"qa_data/jpg/starfish.jpg");
if (success == false) {
wprintf(L"Failed to load image file.\n");
return;
}
CkJsonObjectW json;
json.UpdateString(L"productImage.attachment",bdImage.getEncoded(L"base64"));
json.UpdateString(L"productImage.filename",L"starfish.jpg");
http.SetRequestHeader(L"Content-Type",L"application/json");
// The product ID in the URL is "112265200".
CkHttpResponseW resp;
success = http.HttpJson(L"POST",L"https://api.webshopapp.com/en/products/112265200/images.json",json,L"application/json",resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
CkStringBuilderW sbResponseBody;
resp.GetBodySb(sbResponseBody);
CkJsonObjectW jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",jResp.emit());
int respStatusCode = resp.get_StatusCode();
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",resp.header());
wprintf(L"Failed.\n");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "productImage": {
// "id": 13362569,
// "sortOrder": 3,
// "createdAt": "2019-06-06T14:52:07+00:00",
// "updatedAt": "2019-06-06T14:52:07+00:00",
// "extension": "png",
// "size": 8016,
// "title": "logo",
// "thumb": "https://cdn.shoplightspeed.com/shops/000001/files/14271562/50x50x2/logo.png",
// "src": "https://cdn.shoplightspeed.com/shops/000001/files/14271562/logo.png"
// }
// }
// 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 productImageId = jResp.IntOf(L"productImage.id");
int productImageSortOrder = jResp.IntOf(L"productImage.sortOrder");
const wchar_t *productImageCreatedAt = jResp.stringOf(L"productImage.createdAt");
const wchar_t *productImageUpdatedAt = jResp.stringOf(L"productImage.updatedAt");
const wchar_t *productImageExtension = jResp.stringOf(L"productImage.extension");
int productImageSize = jResp.IntOf(L"productImage.size");
const wchar_t *productImageTitle = jResp.stringOf(L"productImage.title");
const wchar_t *productImageThumb = jResp.stringOf(L"productImage.thumb");
const wchar_t *productImageSrc = jResp.stringOf(L"productImage.src");
}