Sample code for 30+ languages & platforms
Rust

Shopware 6 - Create Product

See more Shopware 6 Examples

Create a new product.

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

// Sends the following POST

// POST /api/v3/product/
// {
//     "name" : "Test123",
//     "productNumber" : "random",
//     "stock" : 10,
//     "price" : [
//         {
//             "currencyId" : "b7d2554b0ce847cd82f3ac9bd1c0dfca", 
//             "gross": 15, 
//             "net": 10, 
//             "linked" : false
//         }
//     ],
//     "tax" : {
//         "name": "test", 
//         "taxRate": 15
//     }    
// }

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

// Create a product named "Test123"
let json = chilkat::JsonObject::new();
let _ = json.update_string("name", "Test123");
let _ = json.update_string("productNumber", "XYZ-1234");
let _ = json.update_int("stock", 10);
let _ = json.update_string("price[0].currencyId", "b7d2554b0ce847cd82f3ac9bd1c0dfca");
let _ = json.update_int("price[0].gross", 15);
let _ = json.update_int("price[0].net", 10);
let _ = json.update_bool("price[0].linked", false);
let _ = json.update_string("tax.name", "test");
let _ = json.update_int("tax.taxRate", 15);

// Load the access token previously obtained in Shopware 6 OAuth2 Client Credentials
let json_token = chilkat::JsonObject::new();
let _ = json_token.load_file("qa_data/tokens/shopware6.json");

// This causes the "Authorization: Bearer <access_token>" header to be added.
http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());

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

// A successful response is a 204 response status code with an empty response body.
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());

// If we get a 401 response, it may be that our access token expired and we need to fetch a new one.
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;
}