Sample code for 30+ languages & platforms
Rust

Square API - Create Catalog Image

See more Square Examples

Uploads an image file to be represented by an CatalogImage object linked to an existing CatalogObject instance.

Chilkat Rust Downloads

Rust

// 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 https://connect.squareup.com/v2/catalog/images \
//   -X POST \
//   -H 'Square-Version: 2020-07-22' \
//   -H 'Authorization: Bearer ACCESS_TOKEN' \
//   -H 'Accept: application/json' \
//   -F 'file=@/local/path/to/file.jpg' \
//   -F 'request={
//     "idempotency_key": "528dea59-7bfb-43c1-bd48-4a6bba7dd61f86",
//     "object_id": "ND6EA5AAJEO5WL3JNNIAQA32",
//     "image": {
//       "id": "#TEMP_ID",
//       "type": "IMAGE",
//       "image_data": {
//         "caption": "A picture of a cup of coffee"
//       }
//     }
//   }'

// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code

let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/v2/catalog/images");
req.set_content_type("multipart/form-data");
let _ = req.add_file_for_upload2("file", "/local/path/to/file.jpg", "image/jpeg").is_ok();

// Make sure to use an object_id for an already-existing catalog item.
let json = chilkat::JsonObject::new();
let _ = json.update_string("idempotency_key", "528dea59-7bfb-43c1-bd48-4a6bba7dd61f86");
let _ = json.update_string("object_id", "2PTZYUOFGGDMT75UZLHQAPAC");
let _ = json.update_string("image.id", "#TEMP_ID");
let _ = json.update_string("image.type", "IMAGE");
let _ = json.update_string("image.image_data.caption", "A picture of a cup of coffee");

req.add_param("request", &json.emit().unwrap_or_default());

req.add_header("Expect", "100-continue");
req.add_header("Authorization", "Bearer ACCESS_TOKEN");
req.add_header("Accept", "application/json");
req.add_header("Square-Version", "2020-07-22");

// This example uses the sandbox: connect.squareupsandbox.com
// Production should use connect.squareup.com
let resp = chilkat::HttpResponse::new();
if http.http_s_req("connect.squareupsandbox.com", 443, true, &req, &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)

// {
//   "image": {
//     "type": "IMAGE",
//     "id": "UBXKMBQ4QK47QXMNQD6ELCZT",
//     "updated_at": "2020-08-07T15:20:09.779Z",
//     "version": 1596813609779,
//     "is_deleted": false,
//     "present_at_all_locations": true,
//     "image_data": {
//       "name": "",
//       "url": "https://square-catalog-sandbox.s3.amazonaws.com/files/168973eab6f8b39b5cbdad52e3f82b23be196e2b/original.jpeg",
//       "caption": "A picture of a cup of coffee"
//     }
//   }
// }

// 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 image_type = j_resp.string_of("image.type").unwrap_or_default();
let image_id = j_resp.string_of("image.id").unwrap_or_default();
let image_updated_at = j_resp.string_of("image.updated_at").unwrap_or_default();
let image_version = j_resp.int_of("image.version");
let image_is_deleted = j_resp.bool_of("image.is_deleted");
let image_present_at_all_locations = j_resp.bool_of("image.present_at_all_locations");
let image_image_data_name = j_resp.string_of("image.image_data.name").unwrap_or_default();
let image_image_data_url = j_resp.string_of("image.image_data.url").unwrap_or_default();
let image_image_data_caption = j_resp.string_of("image.image_data.caption").unwrap_or_default();