Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Lua/CallWebService.lua @ 4299:3f85db78c441
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 05 Nov 2020 18:24:50 +0100 |
parents | d25f4c0fa160 |
children |
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 | 8 |
9 -- NOTE : Replace "load" by "loadstring" for Lua <= 5.1 | |
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 |