Sample code for 30+ languages & platforms
Rust

Shopware Upload Media (JPG File)

See more Shopware Examples

Demonstrates how to upload a media file to a Shopware shop.

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.

// {
//   "file": "data:image/jpeg;base64,BASE64_DATA_HERE",
//   "album": -9,
//   "description": "image description",
//   "userId": 5,
//   "name": "my_image"
// }

let sb_file_data = chilkat::StringBuilder::new();
let _ = sb_file_data.append("data:image/jpeg;base64,");
let bd_file_data = chilkat::BinData::new();
if bd_file_data.load_file("qa_data/jpg/starfish.jpg").is_err() {
    println!("Failed to load file.");
    return;
}

let _ = sb_file_data.append(&bd_file_data.get_encoded("base64").unwrap_or_default());

let json = chilkat::JsonObject::new();
let _ = json.update_string("file", &sb_file_data.get_as_string().unwrap_or_default());
let _ = json.update_int("album", -9);
let _ = json.update_string("description", "image description");
let _ = json.update_int("userId", 5);
let _ = json.update_string("name", "my_image");

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

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