Rust
Rust
MedTunnel: Send Message with File Attachment
See more MedTunnel Examples
Demonstrates the MedTunnel SendMessage method to send a message with a file attachment to one or more recipients.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Implements the following CURL command:
// curl https://server.medtunnel.com/MedTunnelMsg/api/Message/SendMessage -X POST -k
// -F "ApplicationId=yourApplicationId" -F "LocationId=yourLocationId"
// -F "MedTunnelId=yourMedTunnelId" -F "MedTunnelPassword=yourMedTunnelPassword"
// -F "To=recipientsMedTunnelId"
// -F "Body=Test of SendMessage"
// -F "file1=@qa_data/jpg/starfish.jpg"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/MedTunnelMsg/api/Message/SendMessage");
req.set_content_type("multipart/form-data");
req.add_param("ApplicationId", "yourApplicationId");
req.add_param("LocationId", "yourLocationId");
req.add_param("MedTunnelId", "yourMedTunnelId");
req.add_param("MedTunnelPassword", "yourMedTunnelPassword");
req.add_param("To", "recipientsMedTunnelId");
req.add_param("Body", "Test");
let _ = req.add_file_for_upload2("file1", "qa_data/jpg/starfish.jpg", "application/octet-stream").is_ok();
req.add_header("Expect", "100-continue");
let resp = chilkat::HttpResponse::new();
if http.http_s_req("server.medtunnel.com", 443, true, &req, &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());
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)
// {
// "ReturnCode": 1,
// "ReturnCodeText": "Success",
// "Data": "{\"Id\":989432,\"FromUserId\":36990,\"FromMailBoxId\":36965, ... \"SendGlobalNotifications\":false}"
// }
// 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 return_code = j_resp.int_of("ReturnCode");
let return_code_text = j_resp.string_of("ReturnCodeText").unwrap_or_default();
let data = j_resp.string_of("Data").unwrap_or_default();
// Load the Data into another JSON object and parse..
let json_data = chilkat::JsonObject::new();
let _ = json_data.load(&data);
json_data.set_emit_compact(false);
println!("{}", json_data.emit().unwrap_or_default());
// {
// "Id": 989448,
// "FromUserId": 36990,
// "FromMailBoxId": 36965,
// "FromUserType": 0,
// "FromUserName": "...",
// "FromUserFullName": "...",
// "FromUserAccountName": "...",
// "FromUserAccountTitle": "...",
// "ToUserId": 36990,
// "ToUserType": 1,
// "ToUserMailboxId": "36965",
// "ToUserName": "...",
// "ToUserFullName": "...",
// "EmailAddress": "",
// "Password": "",
// "Subject": "",
// "PatientMedTunnelId": "",
// "Body": "Test",
// "DateReceived": "4/29/2021 2:48:22 PM",
// "DisplayDateReceived": "04/29/2021 2:48 PM",
// "ViewCount": 0,
// "ViewedOn": "",
// "AttachmentCount": 1,
// "AttachmentNames": [
// {
// "MessageId": 989448,
// "Id": 424857,
// "Name": "starfish.jpg.35910fe9-4118-414c-a845-4d092ca6e784",
// "DisplayName": "starfish.jpg",
// "Size": 6229,
// "WasViewed": false,
// "ViewedOn": "",
// "ViewCount": 0,
// "Location": "Default"
// }
// ],
// "AllRecipients": [
// {
// "Id": 989448,
// "UserName": "...",
// "AccountId": 0,
// "AccountName": "...",
// "AccountTitle": "",
// "FirstName": "...",
// "LastName": "...",
// "EmailAddress": "",
// "LastSentOn": "",
// "SendCount": 0,
// "IsFavorite": false
// }
// ],
// "Status": 0,
// "ParentMessageId": 989448,
// "DistributionListId": 0,
// "DistributionListName": "",
// "BodyHistory": "",
// "ReadReceiptCallbackUrl": null,
// "SendGlobalNotifications": false
// }
let mut id = json_data.int_of("Id");
let from_user_id = json_data.int_of("FromUserId");
let from_mail_box_id = json_data.int_of("FromMailBoxId");
let from_user_type = json_data.int_of("FromUserType");
let from_user_name = json_data.string_of("FromUserName").unwrap_or_default();
let from_user_full_name = json_data.string_of("FromUserFullName").unwrap_or_default();
let from_user_account_name = json_data.string_of("FromUserAccountName").unwrap_or_default();
let from_user_account_title = json_data.string_of("FromUserAccountTitle").unwrap_or_default();
let to_user_id = json_data.int_of("ToUserId");
let to_user_type = json_data.int_of("ToUserType");
let to_user_mailbox_id = json_data.string_of("ToUserMailboxId").unwrap_or_default();
let to_user_name = json_data.string_of("ToUserName").unwrap_or_default();
let to_user_full_name = json_data.string_of("ToUserFullName").unwrap_or_default();
let mut email_address = json_data.string_of("EmailAddress").unwrap_or_default();
let password = json_data.string_of("Password").unwrap_or_default();
let subject = json_data.string_of("Subject").unwrap_or_default();
let patient_med_tunnel_id = json_data.string_of("PatientMedTunnelId").unwrap_or_default();
let body = json_data.string_of("Body").unwrap_or_default();
let date_received = json_data.string_of("DateReceived").unwrap_or_default();
let display_date_received = json_data.string_of("DisplayDateReceived").unwrap_or_default();
let mut view_count = json_data.int_of("ViewCount");
let mut viewed_on = json_data.string_of("ViewedOn").unwrap_or_default();
let attachment_count = json_data.int_of("AttachmentCount");
let status = json_data.int_of("Status");
let parent_message_id = json_data.int_of("ParentMessageId");
let distribution_list_id = json_data.int_of("DistributionListId");
let distribution_list_name = json_data.string_of("DistributionListName").unwrap_or_default();
let body_history = json_data.string_of("BodyHistory").unwrap_or_default();
let read_receipt_callback_url = json_data.string_of("ReadReceiptCallbackUrl").unwrap_or_default();
let send_global_notifications = json_data.bool_of("SendGlobalNotifications");
let mut i = 0;
let mut count_i = json_data.size_of_array("AttachmentNames");
while i < count_i {
json_data.set_i(i);
let _ = json_data.int_of("AttachmentNames[i].MessageId");
id = json_data.int_of("AttachmentNames[i].Id");
let _ = json_data.string_of("AttachmentNames[i].Name").unwrap_or_default();
let _ = json_data.string_of("AttachmentNames[i].DisplayName").unwrap_or_default();
let _ = json_data.int_of("AttachmentNames[i].Size");
let _ = json_data.bool_of("AttachmentNames[i].WasViewed");
viewed_on = json_data.string_of("AttachmentNames[i].ViewedOn").unwrap_or_default();
view_count = json_data.int_of("AttachmentNames[i].ViewCount");
let _ = json_data.string_of("AttachmentNames[i].Location").unwrap_or_default();
i = i + 1;
}
i = 0;
count_i = json_data.size_of_array("AllRecipients");
while i < count_i {
json_data.set_i(i);
id = json_data.int_of("AllRecipients[i].Id");
let _ = json_data.string_of("AllRecipients[i].UserName").unwrap_or_default();
let _ = json_data.int_of("AllRecipients[i].AccountId");
let _ = json_data.string_of("AllRecipients[i].AccountName").unwrap_or_default();
let _ = json_data.string_of("AllRecipients[i].AccountTitle").unwrap_or_default();
let _ = json_data.string_of("AllRecipients[i].FirstName").unwrap_or_default();
let _ = json_data.string_of("AllRecipients[i].LastName").unwrap_or_default();
email_address = json_data.string_of("AllRecipients[i].EmailAddress").unwrap_or_default();
let _ = json_data.string_of("AllRecipients[i].LastSentOn").unwrap_or_default();
let _ = json_data.int_of("AllRecipients[i].SendCount");
let _ = json_data.bool_of("AllRecipients[i].IsFavorite");
i = i + 1;
}