Lua API of Attachment Upload

Hello gents,

curl https://mydomain:8042/studies/aa0c1457-b51c6cba-c5bac6fd-65a596c1-b70db111/attachments/reportPdf -X PUT --data-binary @/etc/orthanc/ClientRpts/Sumalia01/20240606210238_Sumalia01.pdf

This command works perfect!

However when I tried to make it into Lua script

local attachmenName = ‘/etc/orthanc/ClientRpts/Sumalia01/20240606210238_Sumalia01.pdf’
local studyId = ‘aa0c1457-b51c6cba-c5bac6fd-65a596c1-b70db111’
UploadAttachment(studyId, attachmenName)

function UploadAttachment(studyId, attachmenName)
local studyAttachmentUrl = ‘/studies/’ … studyId … ‘/attachments/reportPdf’
local payload = attachmenName
RestApiPut(studyAttachmentUrl, payload, true)
end

It doesn’t work.

Thanks in advance

DISCLAIMER: I only know about Python, not Lua, so take my reply with a grain of salt.

Aren’t you by chance, with this code, uploading the name of your attachment instead of its contents? How about the following:

function UploadAttachment(studyId, attachmentName)
  local studyAttachmentUrl = '/studies/' .. studyId .. '/attachments/reportPdf'
  local payload = ReadFile(attachmentName)
  RestApiPut(studyAttachmentUrl, payload, true)
end

function ReadFile(filePath)
  local file = assert(io.open(filePath, "rb"))
  local content = file:read("*all")
  file:close()
  return content
end
1 Like

wow!!
“a grain of salt” fixed my issue.
I really appreciate you going the extra mile and providing the Lua coding!

1 Like