React Native
React Native
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 React Native Downloads
import { Http, HttpRequest, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
// 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
const req = new HttpRequest();
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');
const resp = new HttpResponse();
try {
await http.httpSReqAsync('server.medtunnel.com', 443, true, req, resp);
} catch {
console.log(http.lastErrorText);
return;
}
const sbResponseBody = new StringBuilder();
resp.getBodySb(sbResponseBody);
const jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.emitCompact = false;
console.log('Response Body:');
console.log(jResp.emit());
const respStatusCode = resp.statusCode;
console.log(`Response Status Code = ${respStatusCode}`);
if (respStatusCode >= 400) {
console.log('Response Header:');
console.log(resp.header);
console.log('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
const returnCode = jResp.intOf('ReturnCode');
const returnCodeText = jResp.stringOf('ReturnCodeText');
const data = jResp.stringOf('Data');
// Load the Data into another JSON object and parse..
const jsonData = new JsonObject();
jsonData.load(data);
jsonData.emitCompact = false;
console.log(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
// }
let messageId = 0;
let name = '';
let displayName = '';
let size = 0;
let location = '';
let userName = '';
let accountId = 0;
let accountName = '';
let accountTitle = '';
let firstName = '';
let lastName = '';
let lastSentOn = '';
let sendCount = 0;
let id = jsonData.intOf('Id');
const fromUserId = jsonData.intOf('FromUserId');
const fromMailBoxId = jsonData.intOf('FromMailBoxId');
const fromUserType = jsonData.intOf('FromUserType');
const fromUserName = jsonData.stringOf('FromUserName');
const fromUserFullName = jsonData.stringOf('FromUserFullName');
const fromUserAccountName = jsonData.stringOf('FromUserAccountName');
const fromUserAccountTitle = jsonData.stringOf('FromUserAccountTitle');
const toUserId = jsonData.intOf('ToUserId');
const toUserType = jsonData.intOf('ToUserType');
const toUserMailboxId = jsonData.stringOf('ToUserMailboxId');
const toUserName = jsonData.stringOf('ToUserName');
const toUserFullName = jsonData.stringOf('ToUserFullName');
let emailAddress = jsonData.stringOf('EmailAddress');
const password = jsonData.stringOf('Password');
const subject = jsonData.stringOf('Subject');
const patientMedTunnelId = jsonData.stringOf('PatientMedTunnelId');
const body = jsonData.stringOf('Body');
const dateReceived = jsonData.stringOf('DateReceived');
const displayDateReceived = jsonData.stringOf('DisplayDateReceived');
let viewCount = jsonData.intOf('ViewCount');
let viewedOn = jsonData.stringOf('ViewedOn');
const attachmentCount = jsonData.intOf('AttachmentCount');
const status = jsonData.intOf('Status');
const parentMessageId = jsonData.intOf('ParentMessageId');
const distributionListId = jsonData.intOf('DistributionListId');
const distributionListName = jsonData.stringOf('DistributionListName');
const bodyHistory = jsonData.stringOf('BodyHistory');
const readReceiptCallbackUrl = jsonData.stringOf('ReadReceiptCallbackUrl');
let i = 0;
let 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++;
}
}