Rust
Rust
Enumerate REST Response Header Names
See more REST Examples
Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.
Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.
Chilkat Rust Downloads
let rest = chilkat::Rest::new();
let b_tls = true;
let b_auto_reconnect = true;
if rest.connect("example.com", 443, b_tls, b_auto_reconnect).is_err() {
println!("{}", rest.last_error_text());
return;
}
if rest.send_req_no_body("GET", "/api/items/123").is_err() {
println!("{}", rest.last_error_text());
return;
}
let status_code = rest.read_response_header();
if status_code < 0 {
println!("{}", rest.last_error_text());
return;
}
// Enumerate the response header fields by zero-based index. ResponseHdrName returns the field name
// and ResponseHdrValue returns the value at the same index.
let num_headers = rest.num_response_headers();
for i in 0..num_headers {
let Ok(name) = rest.response_hdr_name(i) else {
println!("{}", rest.last_error_text());
return;
};
let Ok(value) = rest.response_hdr_value(i) else {
println!("{}", rest.last_error_text());
return;
};
println!("{}: {}", name, value);
}