Page 1 of 1

Rest API (V15) multipart/form-data file upload with JWT not supported?

Posted: Tue Jan 19, 2021 2:17 am
by marcel.heij
I am trying to create a srvroutine for file upload via multipart/form-data in combination with JWT security. However it seems the TryBind method does not support multipart/form-data. Is that correct? Does somebody have an example of a file upload API with JWT security?

Re: Rest API (V15) multipart/form-data file upload with JWT not supported?

Posted: Wed Jan 20, 2021 5:31 am
by marcel.heij
Well, my question is still stands, but I do have a workaround without the TryBind method unfortunately:

* ================================================================================= *
Srvroutine Name(UploadFile) Desc('Save a Blob on de server as a file') Response(*HTTP #Context)
Define Field(#TokenFromHeader) Type(*NVARCHAR)

* Get the HTTP header with the name 'Authorization' and assign to the just defined field
#Context.Request.Headers.TryGetString("Authorization" #TokenFromHeader)

* Take the #TokenFromHeader and trim off the 'Bearer ' string at the beginning to just pass the token string to the TryVerifyToken method
If (#HSnnn.TryVerifyToken( #TokenFromHeader.LeftTrim("Bearer "), #jwt, "YourSecret" ))

* Get the multipart/form-data variables including the file itself
#S_ClientAgreementID := #Context.Request.Content.FormVariables<"ClientAgreementID">.Value
#FileType := #Context.Request.Content.FormVariables<"FiletypeCode">.Value
#DF_ELFNAM := #Context.Request.Content.FormVariables<"File">.FileName

<< rest of the code >>
Endif
Endroutine
* ================================================================================= *
This seems to work, but is it the right way to do it? Does the TryBind method need more development from Lansa or do I miss something?