Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Json
oleobject loo_SbRequestBody
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
integer li_ReturnCode
string ls_ReturnCodeText
string ls_Data
oleobject loo_JsonData
string ls_Token

li_Success = 0

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// 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"
// }

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("MedTunnelId","yourMedTunnelId@yourGroup")
loo_Json.UpdateString("Password","yourPassword")
loo_Json.UpdateString("ApplicationId","yourApplicationId")
loo_Json.UpdateString("LocationId","yourLocationId")
loo_Json.UpdateString("ExpirationInMinutes","50")

loo_Http.SetRequestHeader("Content-Type","application/json")

loo_SbRequestBody = create oleobject
li_rc = loo_SbRequestBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Json.EmitSb(loo_SbRequestBody)

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpSb("PUT","https://server.medtunnel.com/MedTunnelSvc/api/Authenticate/LoginUser",loo_SbRequestBody,"utf-8","application/json",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    destroy loo_SbRequestBody
    destroy loo_Resp
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Json
    destroy loo_SbRequestBody
    destroy loo_Resp
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

// 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

li_ReturnCode = loo_JResp.IntOf("ReturnCode")
ls_ReturnCodeText = loo_JResp.StringOf("ReturnCodeText")
ls_Data = loo_JResp.StringOf("Data")

// Load the Data into another JSON object and parse..
loo_JsonData = create oleobject
li_rc = loo_JsonData.ConnectToNewObject("Chilkat.JsonObject")

loo_JsonData.Load(ls_Data)
loo_JsonData.EmitCompact = 0
Write-Debug loo_JsonData.Emit()

// 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"
// }

ls_Token = loo_JsonData.StringOf("Token")
Write-Debug "Token: " + ls_Token


destroy loo_Http
destroy loo_Json
destroy loo_SbRequestBody
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp
destroy loo_JsonData