Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// 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 = CkoHttpRequest()!
req.httpVerb = "POST"
req.path = "/MedTunnelMsg/api/Message/SendMessage"
req.contentType = "multipart/form-data"
req.addParam(name: "ApplicationId", value: "yourApplicationId")
req.addParam(name: "LocationId", value: "yourLocationId")
req.addParam(name: "MedTunnelId", value: "yourMedTunnelId")
req.addParam(name: "MedTunnelPassword", value: "yourMedTunnelPassword")
req.addParam(name: "To", value: "recipientsMedTunnelId")
req.addParam(name: "Body", value: "Test")
success = req.addFile(forUpload2: "file1", path: "qa_data/jpg/starfish.jpg", contentType: "application/octet-stream")
req.addHeader(name: "Expect", value: "100-continue")
let resp = CkoHttpResponse()!
success = http.httpSReq(domain: "server.medtunnel.com", port: 443, ssl: true, request: req, response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp.getBodySb(sb: sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = resp.statusCode.intValue
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
var ReturnCode: Int = jResp.int(of: "ReturnCode").intValue
var ReturnCodeText: String? = jResp.string(of: "ReturnCodeText")
var Data: String? = jResp.string(of: "Data")
// Load the Data into another JSON object and parse..
let jsonData = CkoJsonObject()!
jsonData.load(json: 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: Int
var Name: String?
var DisplayName: String?
var Size: Int
var WasViewed: Bool
var Location: String?
var UserName: String?
var AccountId: Int
var AccountName: String?
var AccountTitle: String?
var FirstName: String?
var LastName: String?
var LastSentOn: String?
var SendCount: Int
var IsFavorite: Bool
var Id: Int = jsonData.int(of: "Id").intValue
var FromUserId: Int = jsonData.int(of: "FromUserId").intValue
var FromMailBoxId: Int = jsonData.int(of: "FromMailBoxId").intValue
var FromUserType: Int = jsonData.int(of: "FromUserType").intValue
var FromUserName: String? = jsonData.string(of: "FromUserName")
var FromUserFullName: String? = jsonData.string(of: "FromUserFullName")
var FromUserAccountName: String? = jsonData.string(of: "FromUserAccountName")
var FromUserAccountTitle: String? = jsonData.string(of: "FromUserAccountTitle")
var ToUserId: Int = jsonData.int(of: "ToUserId").intValue
var ToUserType: Int = jsonData.int(of: "ToUserType").intValue
var ToUserMailboxId: String? = jsonData.string(of: "ToUserMailboxId")
var ToUserName: String? = jsonData.string(of: "ToUserName")
var ToUserFullName: String? = jsonData.string(of: "ToUserFullName")
var EmailAddress: String? = jsonData.string(of: "EmailAddress")
var Password: String? = jsonData.string(of: "Password")
var Subject: String? = jsonData.string(of: "Subject")
var PatientMedTunnelId: String? = jsonData.string(of: "PatientMedTunnelId")
var Body: String? = jsonData.string(of: "Body")
var DateReceived: String? = jsonData.string(of: "DateReceived")
var DisplayDateReceived: String? = jsonData.string(of: "DisplayDateReceived")
var ViewCount: Int = jsonData.int(of: "ViewCount").intValue
var ViewedOn: String? = jsonData.string(of: "ViewedOn")
var AttachmentCount: Int = jsonData.int(of: "AttachmentCount").intValue
var Status: Int = jsonData.int(of: "Status").intValue
var ParentMessageId: Int = jsonData.int(of: "ParentMessageId").intValue
var DistributionListId: Int = jsonData.int(of: "DistributionListId").intValue
var DistributionListName: String? = jsonData.string(of: "DistributionListName")
var BodyHistory: String? = jsonData.string(of: "BodyHistory")
var ReadReceiptCallbackUrl: String? = jsonData.string(of: "ReadReceiptCallbackUrl")
var SendGlobalNotifications: Bool = jsonData.bool(of: "SendGlobalNotifications")
var i: Int = 0
var count_i: Int = jsonData.size(ofArray: "AttachmentNames").intValue
while i < count_i {
jsonData.i = i
MessageId = jsonData.int(of: "AttachmentNames[i].MessageId").intValue
Id = jsonData.int(of: "AttachmentNames[i].Id").intValue
Name = jsonData.string(of: "AttachmentNames[i].Name")
DisplayName = jsonData.string(of: "AttachmentNames[i].DisplayName")
Size = jsonData.int(of: "AttachmentNames[i].Size").intValue
WasViewed = jsonData.bool(of: "AttachmentNames[i].WasViewed")
ViewedOn = jsonData.string(of: "AttachmentNames[i].ViewedOn")
ViewCount = jsonData.int(of: "AttachmentNames[i].ViewCount").intValue
Location = jsonData.string(of: "AttachmentNames[i].Location")
i = i + 1
}
i = 0
count_i = jsonData.size(ofArray: "AllRecipients").intValue
while i < count_i {
jsonData.i = i
Id = jsonData.int(of: "AllRecipients[i].Id").intValue
UserName = jsonData.string(of: "AllRecipients[i].UserName")
AccountId = jsonData.int(of: "AllRecipients[i].AccountId").intValue
AccountName = jsonData.string(of: "AllRecipients[i].AccountName")
AccountTitle = jsonData.string(of: "AllRecipients[i].AccountTitle")
FirstName = jsonData.string(of: "AllRecipients[i].FirstName")
LastName = jsonData.string(of: "AllRecipients[i].LastName")
EmailAddress = jsonData.string(of: "AllRecipients[i].EmailAddress")
LastSentOn = jsonData.string(of: "AllRecipients[i].LastSentOn")
SendCount = jsonData.int(of: "AllRecipients[i].SendCount").intValue
IsFavorite = jsonData.bool(of: "AllRecipients[i].IsFavorite")
i = i + 1
}
}