· Dec 20, 2023

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 vProfile = %request.Get("testprofile")
    set vFilen = %request.GetMimeData("filetest")

Down below is the postman request:

Discussion (5)2
Log in or sign up to continue

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

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! :)