|
| 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**. |
0 commit comments