Sample code for 30+ languages & platforms
Rust

Shopware Create a New Product

See more Shopware Examples

Create a new product in Shopware.

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();

http.set_login("api_username");
http.set_password("api_key");
http.set_basic_auth(true);

// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "name": "Sport Shoes",
//   "active": true,
//   "tax": 19,
//   "supplier": "Sport Shoes Inc.",
//   "categories": [
//     {
//       "id": 384
//     }
//   ],
//   "mainDetail": {
//     "number": "turn",
//     "active": true,
//     "prices": [
//       {
//         "customerGroupKey": "EK",
//         "price": 999
//       }
//     ]
//   }
// }

let json = chilkat::JsonObject::new();
let _ = json.update_string("name", "Sport Shoes");
let _ = json.update_bool("active", true);
let _ = json.update_int("tax", 19);
let _ = json.update_string("supplier", "Sport Shoes Inc.");
let _ = json.update_int("categories[0].id", 384);
let _ = json.update_string("mainDetail.number", "turn");
let _ = json.update_bool("mainDetail.active", true);
let _ = json.update_string("mainDetail.prices[0].customerGroupKey", "EK");
let _ = json.update_int("mainDetail.prices[0].price", 999);

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://my-shopware-shop.com/api/articles", &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());

// A 201 response code indicates success.
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)

// {
//   "success": true,
//   "data": {
//     "id": 8312,
//     "location": "https:\/\/my-shopware-shop.com\/api\/articles\/8312"
//   }
// }

// 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 _ = j_resp.bool_of("success");
let data_id = j_resp.int_of("data.id");
let data_location = j_resp.string_of("data.location").unwrap_or_default();