C++
C++
MedTunnel: Send Message Text Only
See more MedTunnel Examples
Demonstrates the MedTunnel SendMessage method to send a message to one or more recipients.Chilkat C++ Downloads
#include <CkHttp.h>
#include <CkHttpRequest.h>
#include <CkHttpResponse.h>
#include <CkStringBuilder.h>
#include <CkJsonObject.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttp 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"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
CkHttpRequest req;
req.put_HttpVerb("POST");
req.put_Path("/MedTunnelMsg/api/Message/SendMessage");
req.put_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");
CkHttpResponse resp;
success = http.HttpSReq("server.medtunnel.com",443,true,req,resp);
if (success == false) {
std::cout << http.lastErrorText() << "\r\n";
return;
}
CkStringBuilder sbResponseBody;
resp.GetBodySb(sbResponseBody);
CkJsonObject jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
std::cout << "Response Body:" << "\r\n";
std::cout << jResp.emit() << "\r\n";
int respStatusCode = resp.get_StatusCode();
std::cout << "Response Status Code = " << respStatusCode << "\r\n";
if (respStatusCode >= 400) {
std::cout << "Response Header:" << "\r\n";
std::cout << resp.header() << "\r\n";
std::cout << "Failed." << "\r\n";
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
int ReturnCode = jResp.IntOf("ReturnCode");
const char *ReturnCodeText = jResp.stringOf("ReturnCodeText");
const char *Data = jResp.stringOf("Data");
// Load the Data into another JSON object and parse..
CkJsonObject jsonData;
jsonData.Load(Data);
jsonData.put_EmitCompact(false);
std::cout << jsonData.emit() << "\r\n";
// {
// "Id": 989436,
// "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:29:18 PM",
// "DisplayDateReceived": "04/29/2021 2:29 PM",
// "ViewCount": 0,
// "ViewedOn": "",
// "AttachmentCount": 0,
// "AttachmentNames": [
// ],
// "AllRecipients": [
// {
// "Id": 989436,
// "UserName": "...",
// "AccountId": 0,
// "AccountName": "...",
// "AccountTitle": "",
// "FirstName": "...",
// "LastName": "...",
// "EmailAddress": "",
// "LastSentOn": "",
// "SendCount": 0,
// "IsFavorite": false
// }
// ],
// "Status": 1,
// "ParentMessageId": 989436,
// "DistributionListId": 0,
// "DistributionListName": "",
// "BodyHistory": "",
// "ReadReceiptCallbackUrl": null,
// "SendGlobalNotifications": false
// }
//
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
const char *UserName = 0;
int AccountId;
const char *AccountName = 0;
const char *AccountTitle = 0;
const char *FirstName = 0;
const char *LastName = 0;
const char *LastSentOn = 0;
int SendCount;
bool IsFavorite;
int Id = jsonData.IntOf("Id");
int FromUserId = jsonData.IntOf("FromUserId");
int FromMailBoxId = jsonData.IntOf("FromMailBoxId");
int FromUserType = jsonData.IntOf("FromUserType");
const char *FromUserName = jsonData.stringOf("FromUserName");
const char *FromUserFullName = jsonData.stringOf("FromUserFullName");
const char *FromUserAccountName = jsonData.stringOf("FromUserAccountName");
const char *FromUserAccountTitle = jsonData.stringOf("FromUserAccountTitle");
int ToUserId = jsonData.IntOf("ToUserId");
int ToUserType = jsonData.IntOf("ToUserType");
const char *ToUserMailboxId = jsonData.stringOf("ToUserMailboxId");
const char *ToUserName = jsonData.stringOf("ToUserName");
const char *ToUserFullName = jsonData.stringOf("ToUserFullName");
const char *EmailAddress = jsonData.stringOf("EmailAddress");
const char *Password = jsonData.stringOf("Password");
const char *Subject = jsonData.stringOf("Subject");
const char *PatientMedTunnelId = jsonData.stringOf("PatientMedTunnelId");
const char *Body = jsonData.stringOf("Body");
const char *DateReceived = jsonData.stringOf("DateReceived");
const char *DisplayDateReceived = jsonData.stringOf("DisplayDateReceived");
int ViewCount = jsonData.IntOf("ViewCount");
const char *ViewedOn = jsonData.stringOf("ViewedOn");
int AttachmentCount = jsonData.IntOf("AttachmentCount");
int Status = jsonData.IntOf("Status");
int ParentMessageId = jsonData.IntOf("ParentMessageId");
int DistributionListId = jsonData.IntOf("DistributionListId");
const char *DistributionListName = jsonData.stringOf("DistributionListName");
const char *BodyHistory = jsonData.stringOf("BodyHistory");
const char *ReadReceiptCallbackUrl = jsonData.stringOf("ReadReceiptCallbackUrl");
bool SendGlobalNotifications = jsonData.BoolOf("SendGlobalNotifications");
int i = 0;
int count_i = jsonData.SizeOfArray("AttachmentNames");
while (i < count_i) {
jsonData.put_I(i);
i = i + 1;
}
i = 0;
count_i = jsonData.SizeOfArray("AllRecipients");
while (i < count_i) {
jsonData.put_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");
IsFavorite = jsonData.BoolOf("AllRecipients[i].IsFavorite");
i = i + 1;
}
}