DataFlex
DataFlex
MedTunnel: Login User
See more MedTunnel Examples
Authenticate and create a session for the User. This gets a user token to be used in the Authorization header for other API calls.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Handle hoJson
Variant vSbRequestBody
Handle hoSbRequestBody
Variant vResp
Handle hoResp
Variant vSbResponseBody
Handle hoSbResponseBody
Handle hoJResp
Integer iRespStatusCode
Integer iReturnCode
String sReturnCodeText
String sData
Handle hoJsonData
String sToken
String sTemp1
Move False To iSuccess
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// Implements the following CURL command:
// curl -X PUT -k
// -H "Content-Type: application/json"
// -d '{ "MedTunnelId":"yourMedTunnelId@yourGroup",
// "Password":"yourPassword",
// "ApplicationId":"yourApplicationId",
// "LocationId":"yourLocationId",
// "ExpirationInMinutes":"50"
// }'
// https://server.medtunnel.com/MedTunnelSvc/api/Authenticate/LoginUser
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "MedTunnelId": "yourMedTunnelId@yourGroup",
// "Password": "yourPassword",
// "ApplicationId": "yourApplicationId",
// "LocationId": "yourLocationId",
// "ExpirationInMinutes": "50"
// }
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComUpdateString Of hoJson "MedTunnelId" "yourMedTunnelId@yourGroup" To iSuccess
Get ComUpdateString Of hoJson "Password" "yourPassword" To iSuccess
Get ComUpdateString Of hoJson "ApplicationId" "yourApplicationId" To iSuccess
Get ComUpdateString Of hoJson "LocationId" "yourLocationId" To iSuccess
Get ComUpdateString Of hoJson "ExpirationInMinutes" "50" To iSuccess
Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequestBody
If (Not(IsComObjectCreated(hoSbRequestBody))) Begin
Send CreateComObject of hoSbRequestBody
End
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get ComEmitSb Of hoJson vSbRequestBody To iSuccess
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get pvComObject of hoResp to vResp
Get ComHttpSb Of hoHttp "PUT" "https://server.medtunnel.com/MedTunnelSvc/api/Authenticate/LoginUser" vSbRequestBody "utf-8" "application/json" vResp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
If (Not(IsComObjectCreated(hoJResp))) Begin
Send CreateComObject of hoJResp
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess
Set ComEmitCompact Of hoJResp To False
Showln "Response Body:"
Get ComEmit Of hoJResp To sTemp1
Showln sTemp1
Get ComStatusCode Of hoResp To iRespStatusCode
Showln "Response Status Code = " iRespStatusCode
If (iRespStatusCode >= 400) Begin
Showln "Response Header:"
Get ComHeader Of hoResp To sTemp1
Showln sTemp1
Showln "Failed."
Procedure_Return
End
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "ReturnCode": 1,
// "ReturnCodeText": "Success",
// "Data": "{\"UserId\":36990,\"UserTypeId\":1, ... \"UserRole\":\"Account Admin\"}"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
Get ComIntOf Of hoJResp "ReturnCode" To iReturnCode
Get ComStringOf Of hoJResp "ReturnCodeText" To sReturnCodeText
Get ComStringOf Of hoJResp "Data" To sData
// Load the Data into another JSON object and parse..
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonData
If (Not(IsComObjectCreated(hoJsonData))) Begin
Send CreateComObject of hoJsonData
End
Get ComLoad Of hoJsonData sData To iSuccess
Set ComEmitCompact Of hoJsonData To False
Get ComEmit Of hoJsonData To sTemp1
Showln sTemp1
// Sample result.
// {
// "UserId": 36990,
// "UserTypeId": 1,
// "UserTypeDesc": "Single Provider",
// "Token": "MT 730DCA95-2B86-4A91-994E-99995B1A6FC1:9999952A-C6DA-4FB5-83D6-9999E880B4A2:50:/hc3CTWCS0kNMxZXTWR4NQ==:tev2vAToTCrnuyT6rxyCgQ==:sAYcvgPoIfaaCF0+gdeSD1GFcYk=",
// "MailboxId": 36965,
// "UserRole": "Account Admin"
// }
Get ComStringOf Of hoJsonData "Token" To sToken
Showln "Token: " sToken
End_Procedure