RestDownload lets you retrieve a file with an HTTP GET request from a ViewModel and use the response as a Blob, for example to show a downloaded image in the same ViewModel.
When to use RestDownload
Use selfVM.RestDownload when the endpoint returns file or binary content that must not be handled as text. The operator returns a Blob rather than a String, which preserves data such as images and other binary files.
RestDownload is an OCL operator on selfVM. It is available only in the ViewModel context. It sends an HTTP GET request.
For general REST usage and the role of header nestings, see REST Services in MDriven. For a text-oriented GET request, see RestGet.
Syntax
selfVM.RestDownload(targeturl, user, pwd, optionalnestingwithheaders)The URL is required. The remaining arguments support credentials and a ViewModel nesting that supplies headers. The header nesting is the name of a blue ViewModel class, passed as an OCL String. See REST Services in MDriven for the REST operator pattern.
Download an image into a Blob variable
This example downloads an image from an endpoint and displays the returned Blob in the current ViewModel.
Prepare the ViewModel
- Add a ViewModel variable named
filedownloadedwith type Blob. This variable receives the response from RestDownload. - Add a column, for example
ImageTest, that presents the downloaded value. - Configure
ImageTestwith the Blob image tagged value so that the client treats the Blob as an image. - Add a Download button or action.
Add the button action
Set the button action's OCL expression to assign the returned Blob to the variable:
filedownloaded := selfVM.RestDownload('http://localhost:8182/GetExpectedImg/[GUID]/download', '', '', '')Replace [GUID] with the identifier required by your endpoint. Replace the empty arguments when the service requires credentials or a header nesting.
Show the downloaded image
Set the expression for ImageTest to:
filedownloadedWhen the user selects Download, the GET response is stored in filedownloaded. Because ImageTest evaluates that variable and is configured as a Blob image, it can render the downloaded image.
Verify the endpoint before wiring the ViewModel
Before adding the button action, verify that the URL returns the expected binary stream.
- Call the complete download URL in an HTTP client such as Postman.
- Confirm that the response is successful, for example
200 OK. - Confirm that the client can preview or download the expected file.
- Use the same URL in the RestDownload expression.
For example, a successful request to http://localhost:8182/GetExpectedImg/[GUID]/download can return an image preview. This confirms that the endpoint is reachable and returns binary content before you troubleshoot the ViewModel configuration.
Downloading data from a POST endpoint
RestDownload uses GET. If the service requires POST and returns binary data, use RestPost instead. The documented pattern is to set the base64ReturnStream column in the nesting to true, then convert the returned Base64 value to a Blob. Confirm the exact conversion operator name in your installed MDriven version before implementing this pattern.
