Skip to content

Commit 0eac490

Browse files
author
Johanna Hemminger
authored
Merge pull request mendix#5288 from mendix/jh-rest-binary
Send and Receive Files with REST
2 parents 62c718f + fa5f75a commit 0eac490

6 files changed

Lines changed: 201 additions & 5 deletions

File tree

content/en/docs/refguide/modeling/application-logic/microflows-and-nanoflows/activities/integration-activities/call-rest-action.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ If the [export mapping](/refguide/export-mappings/) is based on a message defini
170170

171171
### 6.2 Binary for the Entire Request
172172

173-
This option allows you to send binary data (for example, the contents of a FileDocument).
173+
This option allows you to send binary data (for example, the contents of a FileDocument). See [Images and Files with REST](/refguide/send-receive-files-rest/) for detailed information working with files with REST).
174174

175175
### 6.3 Form-Data
176176

@@ -202,7 +202,7 @@ These are the options in the drop-down menu for handling the response:
202202

203203
* **Apply import mapping** – if the response is JSON or XML, it can be transformed directly into objects using an [import mapping](/refguide/import-mappings/); the fields that you can choose here are described in the [Import Mapping action](/refguide/import-mapping-action/)
204204
* **Store in an HTTP response** – any successful HTTP response can be stored directly in an [HttpResponse](/refguide/http-request-and-response-entities/#http-response) object, and the [$latestHttpResponse](#latesthttpresponse) variable is also updated
205-
* **Store in a file document** – if the response contains binary content (for example, a PDF), it can be stored in an object of an entity type which inherits from `System.FileDocument`
205+
* **Store in a file document** – if the response contains binary content (for example, a PDF), it can be stored in an object of an entity type which inherits from `System.FileDocument` (see [Images and Files with REST](/refguide/send-receive-files-rest/)) for detailed information working with files with REST)
206206
* **Store in a string** – if the response is a string (for example, CSV), it can be stored directly in a string variable
207207
* **Do not store in a variable** - use this option when the call does not return anything useful
208208

content/en/docs/refguide/modeling/integration/published-rest-services/generate-rest-resource.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ Type the name of the resource that you want to publish.
2626

2727
To be able to create a **Get by key**, a **Patch** or a **Delete** operation, there needs to be a unique attribute on the entity. Select that attribute here.
2828

29-
## 4 Operations
29+
## 4 Operations {#operations}
3030

31-
Check the operations that you want to generate:
31+
Select the check box for the operations that you want to generate:
3232

3333
* **Get all** – allows clients to get all the objects
3434
* **Get by key** – allows clients to get an object, given its key

content/en/docs/refguide/modeling/integration/published-rest-services/published-rest-service/published-rest-operation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The **Method** and **Operation path** define the operation that is executed for
5050

5151
The **Example Location** gives an example of a URL on which the operation can be reached.
5252

53-
#### 2.1.4 Microflow
53+
#### 2.1.4 Microflow {#microflow}
5454

5555
An operation can have the following parameters:
5656

@@ -80,6 +80,7 @@ The result of the microflow is the result of the operation and can include the f
8080
a. Use the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) header to specify the file's MIME type.
8181
b. Use the [Content-Disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) header to specify the file name and to indicate whether the file should be downloaded as an attachment.
8282
c. Use additional HTTP response headers to communicate other information about the file.
83+
See the [Setting Up the MIME Type](/refguide/send-receive-files-rest/#set-mime-type) section of *Publish and Consume Images and Files with REST* for more information.
8384

8485
2. **Return a** ***list*** **or an** ***object*** – specify an export mapping to convert it to XML or JSON.
8586

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
---
2+
title: "Publish and Retrieve Images and Files with REST"
3+
url: /refguide/send-receive-files-rest/
4+
linktitle: "Images and Files with REST"
5+
weight: 79
6+
tags: ["rest", "binary", "send files", "receive files", "OData", "expose", "published REST service", "consume"]
7+
---
8+
9+
## 1 Introduction
10+
11+
This guide covers how to use REST to send and retrieve files, including images, in Studio Pro.
12+
13+
You will learn about the following:
14+
15+
* [Creating a REST service](#create-service) from a **System.Image** or **System.FileDocument** entity
16+
* [Trying out, or testing,](#test-service) the service
17+
* [Setting the MIME type](#set-mime-type) for supported files
18+
* [Retrieving files](#retrieve-files) with REST
19+
20+
If you are only interesting in consuming files, you can skip down to the [Retrieve Images and Files with REST](#retrieve-files) section.
21+
22+
### 1.1 Prerequisites
23+
24+
To publish or retrieve files with REST, do the following:
25+
26+
* Install Studio Pro
27+
28+
We recommend reading the following for some more background:
29+
30+
* [Publishing](/howto/integration/publish-rest-service/) and [consuming](/howto/integration/consume-a-rest-service/) REST services
31+
* Working with [Images, Videos and Files](/refguide/image-and-file-widgets/) in Studio Pro
32+
33+
## 2 Send Files with REST {#create-service}
34+
35+
Imagine that your app is functioning as a content management system (CMS), and you want to be able to send and receive images and files. You can publish the generalized entities as a REST service, which exposes the binary data of files stored in the entity.
36+
37+
### 2.1 Publishing the Service {#publish-service}
38+
39+
To publish the **System.Image** or **System.Filedocument** entities as a [REST service](/refguide/published-rest-service/), do the following:
40+
41+
1. Add a new module (or use **MyFirstModule**) and name it **CMS**.
42+
43+
2. Go to the domain model and create a new entity called *MyFile*.
44+
45+
3. Add a **System.Image** or **System.FileDocument** [generalization](/refguide/entities/#generalization) to the *MyFile* entity
46+
47+
{{< figure src="/attachments/refguide/modeling/integration/rest-binary/starting-entity.png" >}}
48+
49+
4. Generate the overview pages for the file or image entity, then link them to the home page and navigation.
50+
* Right click on the **System.Image** or **System.FileDocument** entity that you want to publish, and click **Generate overview pages**.
51+
* Add a button to the home page that links to the **Overview** page you created.
52+
53+
5. Expose the entity as a REST resource.
54+
* Right-click on the **System.Image** or **System.FileDocument** entity that contains the file(s) that you want to publish, and click **Expose as a REST resource**.
55+
* Click **Select** next to the **Service** field, then click on the folder where you want to create the service and click **New**. Enter a name for the REST service and click **OK**.
56+
57+
6. Back in the **Generate resource and operations** window, select **MyFileID** as the **Key attribute** and check the boxes for the following **Operations***:
58+
* **Get all**
59+
* **Get by key**
60+
* **Post (Create)**
61+
* **Delete**
62+
63+
See the [Operations](/refguide/generate-rest-resource/#operations) section of *Generating a Published REST Resource* for a description of each operation.
64+
65+
7. Click **OK**.
66+
67+
The **Published REST service** document for the exposed image or file entity has been created and is now open on your screen.
68+
69+
After you run your app, click the URL in the **Location** field to view the OpenAPI specs.
70+
71+
### 2.1.1 Understanding the Service Details {#service-details}
72+
73+
Open your published REST service, then double-click the **Get by** key, or single-click and click **Edit**, to open the **Get by** property details. By default, the **Get by** key will return a binary response. The **Export mapping** is blank because a binary object without an export mapping returns binary content.
74+
75+
{{< figure src="/attachments/refguide/modeling/integration/rest-binary/no-export-mapping.png" >}}
76+
77+
Click **Show** next to the **Microflow** field to view the **MyFirstModule.MyFile_Get_ByKey** microflow. The generated **Get by** key returns a file document.
78+
79+
### 2.2 Adding the MIME Type to a GET Microflow {#set-mime-type}
80+
81+
A `GET` request to the REST endpoint you created (in this example, `http://localhost:8080/rest/cmsapi/v1/myfile/1`) will return the binary for the first uploaded file. We need to specify the expected media type so that it returns the file in the expected way (for example, displaying an image).
82+
83+
In the **GET_ByKey** microflow, specify the media type (or MIME type) in the content headers. To learn more about content headers in microflows, see the **Return a file document** entry in the [Microflow](/refguide/published-rest-operation/#microflow) section of *Published REST Operation*. Detailed steps are explained below.
84+
85+
See [Common MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types) for a generic, external list of common content header types.
86+
87+
#### 2.3.1 MIME Type for Images {#mime-images}
88+
89+
In the **GET_ByKey** microflow, do the following:
90+
91+
1. Drag a **Create Object** action into your microflow just before the **End Event**.
92+
2. In the **Entity** field, select **System.HTTPHeader**.
93+
3. Under the **Refresh in client** field, click **New**.
94+
4. Set the three drop-down options to the following:
95+
* **Key (String (Unlimited))** – set the **Value** to `'Content-Type'`
96+
* **Value (String (Unlimited))** – set the **Value** to `'image/png'`, or the image type that will be returned
97+
* **System.HttpHeaders (System.HttpMessage)** - set the **Value** to `$httpResponse`
98+
99+
#### 2.3.2 MIME Type for PDFs
100+
101+
Follow the steps in the [MIME Type for Images](#mime-images) section, then set the three drop-down options to the following:
102+
103+
* **Key (String (Unlimited))** – set the **Value** to `'Content-Type'`
104+
* **Value (String (Unlimited))** – set the **Value** to `'application/pdf'`
105+
* **System.HttpHeaders (System.HttpMessage)** - set the **Value** to `$httpResponse`
106+
107+
### 2.4 Testing the Service {#test-service}
108+
109+
Test the service to ensure that it works!
110+
111+
1. Run your app and upload a PNG file.
112+
You can upload the files on the overview pages you created in the previous step, so make sure you have a link to the file overview page on your home page.
113+
114+
2. Back in Studio Pro, open the service document, and click the link to the service to display the OpenAPI document (for example, `http://localhost:8080/rest-doc/rest/cmsapi/v1`).
115+
116+
3. Try it out by clicking **Get/myfile/{myfileid}** > **Try it out** > **Execute**. The contents will display the binary.
117+
You can also test the call in Visual Studio Code using the [REST Client](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) extension by making the following call:
118+
119+
```
120+
GET http://localhost:8080/rest/cmsapi/v1/myfile/1
121+
Accept: image/png`
122+
```
123+
124+
4. Click **Get/myfile/{myfileid}** to return the content, and you should see the PNG displayed. If you only see the binary, make sure that you have [set the MIME Type](#set-mime-type).
125+
126+
## 3 Retrieve Images or Files from a Published REST Service {#retrieve-files}
127+
128+
You can implement a client in your app that will retrieve binary files from any published REST service, from a Mendix app or anywhere else, and store them in a `FileDocument` entity.
129+
130+
1. Right-click on the **File Explorer** and select **Add module**, then rename it as **CMSClient**.
131+
132+
2. Add an entity to handle the retrieved files.
133+
* In the **Domain Model**, right click to add an entity called **RetrievedFile**.
134+
* Double-click on the entity to open the **Properties** and select the **Generalization**. To test with the PNG file, we can use **System.Image**, or **System.FileDocument** if you want to work with a PDF file.
135+
136+
Then, you can retrieve the image with the [Image widget](#image-widget), retrieve a PDF with the [HTML/Javascript snippet widget](#html-snippet) for PDFs, or [retrieve images or PDFs with a microflow](#retrieve-microflow).
137+
138+
### 3.1 Retrieving Images Using the Image Widget {#image-widget}
139+
140+
Retrieve images with the URL of the published REST service by using the [Image](/appstore/widgets/image/) widget available on the Mendix Marketplace.
141+
142+
To do so, do the following:
143+
144+
1. Complete the two steps in the [Retrieve Files with REST](#retrieve-files) section.
145+
2. Download the [Image](/appstore/widgets/image/) widget from the Mendix Marketplace and import it into your app.
146+
3. In the [Toolbox](/refguide/view-menu/#toolbox), click **Widgets** and search for "Image".
147+
4. Drag the **Image** widget onto a page.
148+
5. Double-click the widget you dragged onto your page to open the **Properties**.
149+
6. In the **Data source** field, select the **Image URL** for **Image type**.
150+
7. Paste the location of the file in the REST service (for example, `http://localhost:8080/rest/cmsapi/v1/myfile/1`).
151+
152+
### 3.2 Retrieving PDFs Using the HTML/Javascript Snippet Widget {#html-snippet}
153+
154+
Retrieve PDFs with the URL of the published REST service by using the [HTML/Javascript Snippet](/appstore/widgets/html-javascript-snippet/) widget available on the Mendix Marketplace.
155+
156+
To do so, do the following:
157+
158+
1. Complete the two steps in the [Retrieve Files with REST](#retrieve-files) section.
159+
2. Download the [HTML/Javascript Snippet](/appstore/widgets/html-javascript-snippet/) widget from the Mendix Marketplace and import it into your app.
160+
3. In the [Toolbox](/refguide/view-menu/#toolbox), click **Widgets** and search for "HTMLSnippet".
161+
4. Drag the **HTMLSnippet** widget onto a page.
162+
5. Double-click the widget you dragged onto your page to open the **Properties**.
163+
6. In the **Content Type** field, ensure that **HTML** is selected.
164+
7. In the **Contents** field, paste the HTML that includes the REST endpoint. For example:
165+
166+
```html
167+
<embed src="http://localhost:8080/rest/cmsapi/v1/myfile/1" width="400px" height="400px">
168+
```
169+
170+
### 3.3 Retrieving Files Using a Microflow {#retrieve-microflow}
171+
172+
You can call a REST service in a microflow, then store the binary response in an entity.
173+
174+
Retrieving files in a microflow uses the [Call REST service](/refguide/call-rest-action/) action. For step-by-step instructions for calling a REST service in a microflow, see [Consume a REST Service](/howto/integration/consume-a-rest-service/).
175+
176+
To retrieve files using a microflow, do the following:
177+
178+
1. Complete the two steps in the [Retrieve Files with REST](#retrieve-files) section.
179+
2. Create a **GetImage** (or **GetFile**) microflow.
180+
* Right click in the **CMSClient** module and select **Add microflow**.
181+
182+
3. Drag a **Call REST service** action into the microflow, and set the following properties:
183+
* In the **General** tab:
184+
* **Location** – the URL to your rest service and the specific file (for example, `http://localhost:8080/rest/cmsapi/v1/myfile/1`)
185+
* In the **Response** tab:
186+
* **Response handling** – select **Store in a file document**
187+
* **Store in variable** – select **Yes**
188+
* **Type** – select the **RetrievedFile** entity
189+
* **Variable Name** – enter `GetResponseFile`
190+
191+
4. For images:
192+
Drag a **Change object** action into the microflow after the **Call REST service** action, so that the image viewer will display the retrieved image.
193+
* Double click the newly created action to open the properties.
194+
* In the **Object** field, select the **GetResponseFile** variable for the **RetrievedFile** entity.
195+
* In the **Refresh in client** field, select **Yes**.
138 KB
Loading
6.25 KB
Loading

0 commit comments

Comments
 (0)