annotate Resources/Samples/Lua/CallWebService.lua @ 2947:11f8d72f366f

Lua 5.3.5
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Nov 2018 18:40:51 +0100
parents 65b1ce7cb84f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1055
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 -- This sample shows how to call a remote Web service whenever an
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 -- instance is received by Orthanc. For this sample to work, you have
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 -- to start the "CallWebService.js" script next to this file using
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 -- NodeJs.
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 -- Download and install the JSON module for Lua by Jeffrey Friedl
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 -- http://regex.info/blog/lua/json
2947
11f8d72f366f Lua 5.3.5
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
8
11f8d72f366f Lua 5.3.5
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
9 -- NOTE : Replace "load" by "loadstring" for Lua <= 5.1
11f8d72f366f Lua 5.3.5
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2032
diff changeset
10 JSON = (load(HttpGet('http://regex.info/code/JSON.lua'))) ()
1055
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 SetHttpCredentials('alice', 'alicePassword')
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 function OnStoredInstance(instanceId, tags, metadata)
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 -- Build the POST body
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 local info = {}
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 info['InstanceID'] = instanceId
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 info['PatientName'] = tags['PatientName']
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 info['PatientID'] = tags['PatientID']
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 -- Send the POST request
2032
65b1ce7cb84f Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1055
diff changeset
22 local answer = HttpPost('http://127.0.0.1:8000/', JSON:encode(info))
1055
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 -- The answer equals "ERROR" in case of an error
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 print('Web service called, answer received: ' .. answer)
6f923d52a46c call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 end