Dart
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final http = CkHttp();
// 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
final req = CkHttpRequest();
req.httpVerb = 'POST';
req.path = '/MedTunnelMsg/api/Message/SendMessage';
req.contentType = 'multipart/form-data';
req.addParam('ApplicationId', 'yourApplicationId');
req.addParam('LocationId', 'yourLocationId');
req.addParam('MedTunnelId', 'yourMedTunnelId');
req.addParam('MedTunnelPassword', 'yourMedTunnelPassword');
req.addParam('To', 'recipientsMedTunnelId');
req.addParam('Body', 'Test');
req.addFileForUpload2('file1', 'qa_data/jpg/starfish.jpg', 'application/octet-stream');
req.addHeader('Expect', '100-continue');
final resp = CkHttpResponse();
try {
http.httpSReq('server.medtunnel.com', 443, true, req, resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final sbResponseBody = CkStringBuilder();
resp.getBodySb(sbResponseBody);
final jResp = CkJsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
print('Response Body:');
print(jResp.emit());
final respStatusCode = resp.statusCode;
print('Response Status Code = $respStatusCode');
if (respStatusCode >= 400) {
print('Response Header:');
print(resp.header);
print('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
final returnCode = jResp.intOf('ReturnCode');
final returnCodeText = jResp.stringOf('ReturnCodeText');
final data = jResp.stringOf('Data');
// Load the Data into another JSON object and parse..
final jsonData = CkJsonObject();
jsonData.load(data);
jsonData.emitCompact = false;
print(jsonData.emit());
// {
// "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
// }
var messageId = 0;
var name = '';
var displayName = '';
var size = 0;
var location = '';
var userName = '';
var accountId = 0;
var accountName = '';
var accountTitle = '';
var firstName = '';
var lastName = '';
var lastSentOn = '';
var sendCount = 0;
var id = jsonData.intOf('Id');
final fromUserId = jsonData.intOf('FromUserId');
final fromMailBoxId = jsonData.intOf('FromMailBoxId');
final fromUserType = jsonData.intOf('FromUserType');
final fromUserName = jsonData.stringOf('FromUserName');
final fromUserFullName = jsonData.stringOf('FromUserFullName');
final fromUserAccountName = jsonData.stringOf('FromUserAccountName');
final fromUserAccountTitle = jsonData.stringOf('FromUserAccountTitle');
final toUserId = jsonData.intOf('ToUserId');
final toUserType = jsonData.intOf('ToUserType');
final toUserMailboxId = jsonData.stringOf('ToUserMailboxId');
final toUserName = jsonData.stringOf('ToUserName');
final toUserFullName = jsonData.stringOf('ToUserFullName');
var emailAddress = jsonData.stringOf('EmailAddress');
final password = jsonData.stringOf('Password');
final subject = jsonData.stringOf('Subject');
final patientMedTunnelId = jsonData.stringOf('PatientMedTunnelId');
final body = jsonData.stringOf('Body');
final dateReceived = jsonData.stringOf('DateReceived');
final displayDateReceived = jsonData.stringOf('DisplayDateReceived');
var viewCount = jsonData.intOf('ViewCount');
var viewedOn = jsonData.stringOf('ViewedOn');
final attachmentCount = jsonData.intOf('AttachmentCount');
final status = jsonData.intOf('Status');
final parentMessageId = jsonData.intOf('ParentMessageId');
final distributionListId = jsonData.intOf('DistributionListId');
final distributionListName = jsonData.stringOf('DistributionListName');
final bodyHistory = jsonData.stringOf('BodyHistory');
final readReceiptCallbackUrl = jsonData.stringOf('ReadReceiptCallbackUrl');
var i = 0;
var countI = jsonData.sizeOfArray('AttachmentNames');
while (i < countI) {
jsonData.i = i;
messageId = jsonData.intOf('AttachmentNames[i].MessageId');
id = jsonData.intOf('AttachmentNames[i].Id');
name = jsonData.stringOf('AttachmentNames[i].Name');
displayName = jsonData.stringOf('AttachmentNames[i].DisplayName');
size = jsonData.intOf('AttachmentNames[i].Size');
jsonData.boolOf('AttachmentNames[i].WasViewed');
viewedOn = jsonData.stringOf('AttachmentNames[i].ViewedOn');
viewCount = jsonData.intOf('AttachmentNames[i].ViewCount');
location = jsonData.stringOf('AttachmentNames[i].Location');
i++;
}
i = 0;
countI = jsonData.sizeOfArray('AllRecipients');
while (i < countI) {
jsonData.i = i;
id = jsonData.intOf('AllRecipients[i].Id');
userName = jsonData.stringOf('AllRecipients[i].UserName');
accountId = jsonData.intOf('AllRecipients[i].AccountId');
accountName = jsonData.stringOf('AllRecipients[i].AccountName');
accountTitle = jsonData.stringOf('AllRecipients[i].AccountTitle');
firstName = jsonData.stringOf('AllRecipients[i].FirstName');
lastName = jsonData.stringOf('AllRecipients[i].LastName');
emailAddress = jsonData.stringOf('AllRecipients[i].EmailAddress');
lastSentOn = jsonData.stringOf('AllRecipients[i].LastSentOn');
sendCount = jsonData.intOf('AllRecipients[i].SendCount');
jsonData.boolOf('AllRecipients[i].IsFavorite');
i++;
}
}