How do I retrieve Data from Form-Data from a REST HTTP-request in a REST-service?
Hello everyone!
I am wondering how I can retrieve multipart form-data from a request that is coming into my REST-service.
I am supposed to retrieve a string and a file. The file is being retrieved without issues, but the "testprofile" string is not being retrieved at all.
It just logs a an empty entry.
The request that is sent to my REST-service has the Content-Type: multipart/form-data
Thanks beforehand for the help! :)
Down below is the code I use right now to retrieve the string and file from Form-data Rest HTTP-request to my service.
set vFilen = %request.GetMimeData("filetest")
Down below is the postman request:
have a look to this article:
Upload into a InterSystems IRIS REST API
The form-data is actually resides in MimeData property and the query parameters are in Data property in %request object. You can you use GetMimeData method to retrieve a single from-data or use NextMimeData for series fetch and The form-data value is in the form of stream. Get method is used to get query parameters from the %request.
ClassMethod SampleCode() As %Status { write "query parameter: ",%request.Get("testParam") set mime="" while 1{ set mime = %request.NextMimeData(mime) q:mime="" write mime,! } return $$$OK }
Hello Ashok! :) Thanks for your answer. I am still having trouble getting the value of the string that is being sent into my webservice. The key is testParam, but it doesn't seem to work as it is now with "Get". Do you have any tips on what to do instead? :)
To complete @Ashok Kumar answer, the good way to get file content :
Set source = %request.GetMimeData("file") Set destination=##class(%Stream.FileBinary).%New() Set destination.Filename="/opt/irisbuild/output/"_source.FileName set tSC=destination.CopyFrom(source) //reader open the file set result=destination.%Save()
Okay! So what finally solved it was a discussion with the WRC support.
Within the Postman request, and the form-data; Content-type was specified as text/plain on each part of the form. That is why %request.Data("profile") wouldn't work at first. The content of each part of the form gets sent into the REST-service as a stream.
With this said, instead I had to use this code which finally worked:
set profileStream = %request.GetMimeData("profile")
set vProfile = profileStream.Read()
However, one thing worth noting is that when I did not specify content-type for each part of the form-data, then the original code I used worked: set vProfile = $Get(%request.Data("profile")) .
Thanks to WRC and everyone here who tried to help! :)