Rust
Rust
Lightspeed - Upload an Image
See more Lightspeed Examples
Create (uploads) a new product image.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// 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.set_login("API_KEY");
http.set_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.
// {
// "productImage": {
// "attachment": "base64-encoded-contents",
// "filename": "the-filename-with-extension-goes-here.jpg"
// }
// }
let bd_image = chilkat::BinData::new();
if bd_image.load_file("qa_data/jpg/starfish.jpg").is_err() {
println!("Failed to load image file.");
return;
}
let json = chilkat::JsonObject::new();
let _ = json.update_string("productImage.attachment", &bd_image.get_encoded("base64").unwrap_or_default());
let _ = json.update_string("productImage.filename", "starfish.jpg");
http.set_request_header("Content-Type", "application/json");
// The product ID in the URL is "112265200".
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.webshopapp.com/en/products/112265200/images.json", &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("Failed.");
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
let product_image_id = j_resp.int_of("productImage.id");
let product_image_sort_order = j_resp.int_of("productImage.sortOrder");
let product_image_created_at = j_resp.string_of("productImage.createdAt").unwrap_or_default();
let product_image_updated_at = j_resp.string_of("productImage.updatedAt").unwrap_or_default();
let product_image_extension = j_resp.string_of("productImage.extension").unwrap_or_default();
let product_image_size = j_resp.int_of("productImage.size");
let product_image_title = j_resp.string_of("productImage.title").unwrap_or_default();
let product_image_thumb = j_resp.string_of("productImage.thumb").unwrap_or_default();
let product_image_src = j_resp.string_of("productImage.src").unwrap_or_default();