Issue77

Title lua access to REST-API is null terminated
Priority feature Status resolved
Superseder Nosy List admin
Assigned To
Keywords Orthanc Core

Created on 2017-12-05.18:56:07 by admin, last changed by admin.

Messages
msg346 (view) Author: admin Date: 2017-12-05.18:56:07
[BitBucket user: Amos Onn]
[BitBucket date: 2017-12-05.17:56:07]

When attempting to access the preview of an Instance via the REST-API in a lua script, we only receive the null-terminated portion of the png (usually not much more than the magic value).
msg347 (view) Author: admin Date: 2017-12-14.17:45:17
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2017-12-14.16:45:17]

Please could you provide a sample Lua script in order for us to be sure to work on your precise issue?
msg348 (view) Author: admin Date: 2018-01-04.16:11:38
[BitBucket user: Amos Onn]
[BitBucket date: 2018-01-04.15:11:38]

```
function OnStoredInstance(instanceId, tags, metadata)
    local preview = RestApiGet('/instances/' .. instanceId .. '/preview')
    local headers = {
        ["content-type"]="Image/PNG",
    }
    HttpPost("http://172.20.5.1:1235", preview, headers)
    Delete(SendToModality(instanceId, 'server'))
end
```
This sent a POST request to the server, but only with the first 8 bytes which are the signature of a PNG file.
I looked through the LUA scripting part of the Orthanc source for `RestApiGet`, and it seems to be using the correct method of the LUA C-API (namely, the one that passes a buffer by size and not by null termination). I'm pretty sure I've attempted printing the output the received object and it looked the same; but now that I think of it, perhaps the log function used the null terminator, and `HttpPost` uses it as well?
msg349 (view) Author: admin Date: 2018-07-12.15:54:10
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2018-07-12.13:54:10]

For reference, here is a sample [Flask](http://flask.pocoo.org/) script to mimic the behavior of Amos' HTTP server running on `http://172.20.5.1:1235`:

```
from flask import Flask
from flask import request
app = Flask(__name__)

@app.route('/', methods = [ 'POST' ])
def store():
    with open('/tmp/preview.png', 'wb') as f:
        f.write(request.get_data())
    return 'ok'

if __name__ == "__main__":
    app.run(debug = True, host = '0.0.0.0')
```

This Flask script writes all the HTTP POST requests it receives into file `/tmp/preview.png`. And here is the corresponding Lua script in Orthanc (the Flask script runs by default on `http://localhost:5000/`):

```
function OnStoredInstance(instanceId, tags, metadata)
    local preview = RestApiGet('/instances/' .. instanceId .. '/preview')
    local headers = {
        ['content-type'] = 'image/png',
    }
    HttpPost('http://localhost:5000', preview, headers)
    Delete(instanceId)
end
```
msg350 (view) Author: admin Date: 2018-07-12.15:54:43
[BitBucket user: Sébastien Jodogne]
[BitBucket date: 2018-07-12.13:54:43]

fix issue #77

→ https://hg.orthanc-server.com/orthanc/changeset/c8d369400ae1
History
Date User Action Args
2026-07-29 15:51:21admincreate