Sample code for 30+ languages & platforms
Rust

QR Code Generator via api.qrserver.com REST API

See more HTTP Examples

Demonstrates how to generate a QR code using the api.qrserver.com REST API service.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let http = chilkat::Http::new();

// Send the following GET request to get a binary response.
// The body of the binary response contains the image data for the QR code.
// https://api.qrserver.com/v1/create-qr-code/?data=[URL-encoded-text]&size=[pixels]x[pixels]

let query_params = chilkat::JsonObject::new();
let _ = query_params.update_string("data", "Hello World");
let _ = query_params.update_string("size", "100x100");

// Get a PNG file..
// Possible formats are:
// png
// gif
// jpeg
// jpg
// svg
// eps
// Case matters.  Use lowercase.
let _ = query_params.update_string("format", "png");

// Send the GET request to an endpoint.
// Chilkat will add the url-encoded query params passed in the JSON.
let resp = chilkat::HttpResponse::new();
if http.http_params("GET", "https://api.qrserver.com/v1/create-qr-code/", &query_params, &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

// Did it succeed?
if resp.status_code() != 200 {
    // The response body, if anything, would not be the image data, but would likely be the error text (or HTML, or whatever...)
    println!("{}", resp.body_str());
    println!("Response status = {}", resp.status_code());
    println!("Failed.");
    return;
}

// Success if we get here..
// Save the binary body as the PNG file, or you can get the bytes of the PNG..
let _ = resp.save_body_binary("c:/temp/qa_output/qr_code.png").is_ok();

// Or get the bytes:
let bd = chilkat::BinData::new();
let _ = resp.get_body_bd(&bd);
// Use the bytes in bd...
// See the online reference documentation for the function to access the bytes directly.

println!("Success.");