Sample code for 30+ languages & platforms
Rust

Shopware 6 - Create Category

See more Shopware 6 Examples

Create a new category.

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/category
// {
//     "name": "Test123"
// }

// Create a category named "Test123"
let json = chilkat::JsonObject::new();
let _ = json.update_string("name", "Test123");

// 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/category", &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;
}