Sample code for 30+ languages & platforms
Rust

Shopify Get particular fields of a single product

See more Shopify Examples

Get particular fields of a single product

Chilkat Rust Downloads

Rust

let rest = chilkat::Rest::new();

let _ = rest.set_auth_basic("SHOPIFY_PRIVATE_API_KEY", "SHOPIFY_PRIVATE_API_KEY");

if rest.connect("chilkat.myshopify.com", 443, true, true).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let sb_json = chilkat::StringBuilder::new();
if rest.full_request_no_body_sb("GET", "/admin/products/#{id}.json?fields=id,images,title", &sb_json).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

if rest.response_status_code() != 200 {
    println!("Received error response code: {}", rest.response_status_code());
    println!("Response body:");
    println!("{}", sb_json.get_as_string().unwrap_or_default());
    return;
}

let json = chilkat::JsonObject::new();
let _ = json.load_sb(&sb_json);

// The following code parses the JSON response.
// A sample JSON response is shown below the sample code.
let product_id: i32 = 0;
let mut i: i32 = 0;
let mut product_id: i32 = 0;
let mut j: i32 = 0;
let mut count_j: i32 = 0;

product_id = json.int_of("product.id");
let product_title = json.string_of("product.title").unwrap_or_default();
i = 0;
let count_i = json.size_of_array("product.images");
while i < count_i {
    json.set_i(i);
    let _ = json.int_of("product.images[i].id");
    product_id = json.int_of("product.images[i].product_id");
    let _ = json.int_of("product.images[i].position");
    let _ = json.string_of("product.images[i].created_at").unwrap_or_default();
    let _ = json.string_of("product.images[i].updated_at").unwrap_or_default();
    let _ = json.int_of("product.images[i].width");
    let _ = json.int_of("product.images[i].height");
    let _ = json.string_of("product.images[i].src").unwrap_or_default();
    j = 0;
    count_j = json.size_of_array("product.images[i].variant_ids");
    while j < count_j {
        json.set_j(j);
        let _ = json.int_of("product.images[i].variant_ids[j]");
        j = j + 1;
    }

    i = i + 1;
}

// A sample JSON response body that is parsed by the above code:

// {
//   "product": {
//     "id": 632910392,
//     "title": "IPod Nano - 8GB",
//     "images": [
//       {
//         "id": 850703190,
//         "product_id": 632910392,
//         "position": 1,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:08:02-04:00",
//         "width": 123,
//         "height": 456,
//         "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano.png?v=1506103682",
//         "variant_ids": [
//         ]
//       },
//       {
//         "id": 562641783,
//         "product_id": 632910392,
//         "position": 2,
//         "created_at": "2017-09-22T14:08:02-04:00",
//         "updated_at": "2017-09-22T14:08:02-04:00",
//         "width": 123,
//         "height": 456,
//         "src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/products\/ipod-nano-2.png?v=1506103682",
//         "variant_ids": [
//           808950810
//         ]
//       }
//     ]
//   }
// }

println!("Example Completed.");