You can use RestGet in a ViewModel EAL action to retrieve data from an external REST endpoint and store its response for use in your ViewModel.
What RestGet does
RestGet sends an HTTP GET request to a URL. It is an operator on selfVM, so you can use it only in the ViewModel context.
Use it when the remote service returns information through a URL, such as a JSON document, text value, or other response body. Put URL query parameters directly in the URL.
Syntax
selfVM.RestGet(targeturl, user, pwd, optionalnestingwithheaders)
| Argument | Purpose |
|---|---|
targeturl
|
The complete remote-service URL. Include any query parameters in this string. |
user
|
Authentication user name. Use Bearer here when the service expects a bearer token.
|
pwd
|
Password for basic authentication, or the bearer token when user is Bearer.
|
optionalnestingwithheaders
|
The name, as an OCL string, of a blue ViewModel nesting that supplies additional request headers. Pass an empty string when no nesting is required. |
When both user and pwd are supplied, MDriven builds an authentication request header. With user set to Bearer, the pwd value is sent as the bearer token. The value supplied for user is preserved, which matters when a remote service is case-sensitive.
Retrieve a response
- In your ViewModel, create a string variable to hold the returned response. For example, create
vResult. - Add an action column and enter an EAL expression that assigns the result of
RestGetto that variable. - Run the action and use
vResultin later ViewModel actions or display it in the ViewModel.
For example, this call retrieves a user by including id=12345 as a URL query parameter and authenticates with a bearer token:
vResult := selfVM.RestGet('https://api.example.com/users?id=12345', 'Bearer', '<token>', '')
vResult receives the response returned by the server. Replace the example URL and token with the endpoint and credentials required by your service.
Add request headers
Use the fourth argument when the service requires request headers in addition to authentication. Create a blue ViewModel nesting, add the required header-control columns to it, and pass the nesting name to RestGet.
For example, if your ViewModel contains a nesting named RequestedHeaders, pass its name as follows:
vResult := selfVM.RestGet('https://api.example.com/users?id=12345', '', '', 'RequestedHeaders')
In the nesting, request-header columns use the DEFAULTREQUESTHEADER_ prefix. For a header whose HTTP name contains dashes, use the DEFAULTREQUESTHEADERMINUS_ prefix; underscores in the column name become dashes in the HTTP header name.
For example, a nesting value in DEFAULTREQUESTHEADER_Accept adds an Accept request header. See REST headers and nesting column conventions for the complete naming rules.
Do not use content payload columns with RestGet: GET and DELETE requests send parameters in the URL. The FORCEREQUESTCONTENTTYPE option can force a Content-Type header on a GET request, but it does so by adding empty content and should be avoided unless the remote framework requires it.
Check the HTTP outcome
Add these string variables to the ViewModel when you need to inspect the result of the HTTP request:
| Variable | Contains |
|---|---|
vReturnStatusCode
|
The status returned by the remote service. |
vReturnMessage
|
The reason message returned by the remote service. |
For example, keep both the body and the status information when calling an endpoint:
vResult := selfVM.RestGet('https://api.example.com/users?id=12345', 'Bearer', '<token>', '')
After the call, evaluate vReturnStatusCode and vReturnMessage before performing an action that depends on a successful response.
Choose the right REST operator
| Need | Operator |
|---|---|
| Retrieve a response body with HTTP GET | selfVM.RestGet
|
| Check headers, metadata, or availability without retrieving the body | RestHead |
| Send content to a remote service with HTTP POST | RestPost |
