-
Notifications
You must be signed in to change notification settings - Fork 795
Send and Receive Files with REST #5288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
e69a11f
Create doc and add structure
d464889
A few changes
4ccb0b5
Add anchor, clarify steps
3b66be8
Add a few more images
d5c3758
Fix image links
47b60fa
Fix a step
f8a14e4
Clarify sections
b0e8e73
Almost ready as a first official draft
25876f6
Finish up main steps
9b16025
Addressed a few of Joost's comments
2a72d6d
Update image
976e695
Style fix
0a5849f
Small clarification
4a0bb53
A change
56201d6
A few updates
60574e5
Add a few links
4d4ceca
Cleaning a bit up
8b4e963
Merge branch 'development' into jh-rest-binary
226948b
Small changes and additional notes
6c00b78
Several changes
b1ba08e
Clean up sections
5c30d66
More cleaning
6e8d500
More changes
14e3107
Small change
ea76439
Rewording
0683fa1
Small change
36a4f9a
Remove extra space
38d5cac
Fix links
6cc681f
Update links
e969184
Update links
f246579
Wee change
e913889
Fix link
7a958dc
Fix typo
fa5f75a
Fix another typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
content/en/docs/refguide/modeling/integration/rest-binary.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| --- | ||
| title: "Publish and Retrieve Images and Files with REST" | ||
| url: /refguide/send-receive-files-rest/ | ||
| linktitle: "Images and Files with REST" | ||
| weight: 79 | ||
| tags: ["rest", "binary", "send files", "receive files", "OData", "expose", "published REST service", "consume"] | ||
| --- | ||
|
|
||
| ## 1 Introduction | ||
|
|
||
| This guide covers how to use REST to send and retrieve files, including images, in Studio Pro. | ||
|
|
||
| You will learn about the following: | ||
|
|
||
| * [Creating a REST service](#create-service) from a **System.Image** or **System.FileDocument** entity | ||
| * [Trying out, or testing,](#test-service) the service | ||
| * [Setting the MIME type](#set-mime-type) for supported files | ||
| * [Retrieving files](#retrieve-files) with REST | ||
|
|
||
| If you are only interesting in consuming files, you can skip down to the [Retrieve Images and Files with REST](#retrieve-files) section. | ||
|
|
||
| ### 1.1 Prerequisites | ||
|
|
||
| To publish or retrieve files with REST, do the following: | ||
|
|
||
| * Install Studio Pro | ||
|
|
||
| We recommend reading the following for some more background: | ||
|
|
||
| * [Publishing](/howto/integration/publish-rest-service/) and [consuming](/howto/integration/consume-a-rest-service/) REST services | ||
| * Working with [Images, Videos and Files](/refguide/image-and-file-widgets/) in Studio Pro | ||
|
|
||
| ## 2 Send Files with REST {#create-service} | ||
|
|
||
| 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. | ||
|
|
||
| ### 2.1 Publishing the Service {#publish-service} | ||
|
|
||
| To publish the **System.Image** or **System.Filedocument** entities as a [REST service](/refguide/published-rest-service/), do the following: | ||
|
|
||
| 1. Add a new module (or use **MyFirstModule**) and name it **CMS**. | ||
|
|
||
| 2. Go to the domain model and create a new entity called *MyFile*. | ||
|
|
||
| 3. Add a **System.Image** or **System.FileDocument** [generalization](/refguide/entities/#generalization) to the *MyFile* entity | ||
|
|
||
| {{< figure src="/attachments/refguide/modeling/integration/rest-binary/starting-entity.png" >}} | ||
|
|
||
| 4. Generate the overview pages for the file or image entity, then link them to the home page and navigation. | ||
| * Right click on the **System.Image** or **System.FileDocument** entity that you want to publish, and click **Generate overview pages**. | ||
| * Add a button to the home page that links to the **Overview** page you created. | ||
|
|
||
| 5. Expose the entity as a REST resource. | ||
| * 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**. | ||
| * 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**. | ||
|
|
||
| 6. Back in the **Generate resource and operations** window, select **MyFileID** as the **Key attribute** and check the boxes for the following **Operations***: | ||
| * **Get all** | ||
| * **Get by key** | ||
| * **Post (Create)** | ||
| * **Delete** | ||
|
|
||
| See the [Operations](/refguide/generate-rest-resource/#operations) section of *Generating a Published REST Resource* for a description of each operation. | ||
|
|
||
| 7. Click **OK**. | ||
|
|
||
| The **Published REST service** document for the exposed image or file entity has been created and is now open on your screen. | ||
|
|
||
| After you run your app, click the URL in the **Location** field to view the OpenAPI specs. | ||
|
|
||
| ### 2.1.1 Understanding the Service Details {#service-details} | ||
|
|
||
| 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. | ||
|
|
||
| {{< figure src="/attachments/refguide/modeling/integration/rest-binary/no-export-mapping.png" >}} | ||
|
|
||
| Click **Show** next to the **Microflow** field to view the **MyFirstModule.MyFile_Get_ByKey** microflow. The generated **Get by** key returns a file document. | ||
|
|
||
| ### 2.2 Adding the MIME Type to a GET Microflow {#set-mime-type} | ||
|
|
||
| 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). | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| #### 2.3.1 MIME Type for Images {#mime-images} | ||
|
|
||
| In the **GET_ByKey** microflow, do the following: | ||
|
|
||
| 1. Drag a **Create Object** action into your microflow just before the **End Event**. | ||
| 2. In the **Entity** field, select **System.HTTPHeader**. | ||
| 3. Under the **Refresh in client** field, click **New**. | ||
| 4. Set the three drop-down options to the following: | ||
| * **Key (String (Unlimited))** – set the **Value** to `'Content-Type'` | ||
| * **Value (String (Unlimited))** – set the **Value** to `'image/png'`, or the image type that will be returned | ||
| * **System.HttpHeaders (System.HttpMessage)** - set the **Value** to `$httpResponse` | ||
|
|
||
| #### 2.3.2 MIME Type for PDFs | ||
|
|
||
| Follow the steps in the [MIME Type for Images](#mime-images) section, then set the three drop-down options to the following: | ||
|
|
||
| * **Key (String (Unlimited))** – set the **Value** to `'Content-Type'` | ||
| * **Value (String (Unlimited))** – set the **Value** to `'application/pdf'` | ||
| * **System.HttpHeaders (System.HttpMessage)** - set the **Value** to `$httpResponse` | ||
|
|
||
| ### 2.4 Testing the Service {#test-service} | ||
|
|
||
| Test the service to ensure that it works! | ||
|
|
||
| 1. Run your app and upload a PNG file. | ||
| 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. | ||
|
|
||
| 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`). | ||
|
|
||
| 3. Try it out by clicking **Get/myfile/{myfileid}** > **Try it out** > **Execute**. The contents will display the binary. | ||
| 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: | ||
|
|
||
| ``` | ||
| GET http://localhost:8080/rest/cmsapi/v1/myfile/1 | ||
| Accept: image/png` | ||
| ``` | ||
|
|
||
| 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). | ||
|
|
||
| ## 3 Retrieve Images or Files from a Published REST Service {#retrieve-files} | ||
|
|
||
| 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. | ||
|
|
||
| 1. Right-click on the **File Explorer** and select **Add module**, then rename it as **CMSClient**. | ||
|
|
||
| 2. Add an entity to handle the retrieved files. | ||
| * In the **Domain Model**, right click to add an entity called **RetrievedFile**. | ||
| * 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. | ||
|
|
||
| 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). | ||
|
|
||
| ### 3.1 Retrieving Images Using the Image Widget {#image-widget} | ||
|
|
||
| Retrieve images with the URL of the published REST service by using the [Image](/appstore/widgets/image/) widget available on the Mendix Marketplace. | ||
|
|
||
| To do so, do the following: | ||
|
|
||
| 1. Complete the two steps in the [Retrieve Files with REST](#retrieve-files) section. | ||
| 2. Download the [Image](/appstore/widgets/image/) widget from the Mendix Marketplace and import it into your app. | ||
| 3. In the [Toolbox](/refguide/view-menu/#toolbox), click **Widgets** and search for "Image". | ||
| 4. Drag the **Image** widget onto a page. | ||
| 5. Double-click the widget you dragged onto your page to open the **Properties**. | ||
| 6. In the **Data source** field, select the **Image URL** for **Image type**. | ||
| 7. Paste the location of the file in the REST service (for example, `http://localhost:8080/rest/cmsapi/v1/myfile/1`). | ||
|
|
||
| ### 3.2 Retrieving PDFs Using the HTML/Javascript Snippet Widget {#html-snippet} | ||
|
|
||
| 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. | ||
|
|
||
| To do so, do the following: | ||
|
|
||
| 1. Complete the two steps in the [Retrieve Files with REST](#retrieve-files) section. | ||
| 2. Download the [HTML/Javascript Snippet](/appstore/widgets/html-javascript-snippet/) widget from the Mendix Marketplace and import it into your app. | ||
| 3. In the [Toolbox](/refguide/view-menu/#toolbox), click **Widgets** and search for "HTMLSnippet". | ||
| 4. Drag the **HTMLSnippet** widget onto a page. | ||
| 5. Double-click the widget you dragged onto your page to open the **Properties**. | ||
| 6. In the **Content Type** field, ensure that **HTML** is selected. | ||
| 7. In the **Contents** field, paste the HTML that includes the REST endpoint. For example: | ||
|
|
||
| ```html | ||
| <embed src="http://localhost:8080/rest/cmsapi/v1/myfile/1" width="400px" height="400px"> | ||
| ``` | ||
|
|
||
| ### 3.3 Retrieving Files Using a Microflow {#retrieve-microflow} | ||
|
|
||
| You can call a REST service in a microflow, then store the binary response in an entity. | ||
|
|
||
| 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/). | ||
|
|
||
| To retrieve files using a microflow, do the following: | ||
|
|
||
| 1. Complete the two steps in the [Retrieve Files with REST](#retrieve-files) section. | ||
| 2. Create a **GetImage** (or **GetFile**) microflow. | ||
| * Right click in the **CMSClient** module and select **Add microflow**. | ||
|
|
||
| 3. Drag a **Call REST service** action into the microflow, and set the following properties: | ||
| * In the **General** tab: | ||
| * **Location** – the URL to your rest service and the specific file (for example, `http://localhost:8080/rest/cmsapi/v1/myfile/1`) | ||
| * In the **Response** tab: | ||
| * **Response handling** – select **Store in a file document** | ||
| * **Store in variable** – select **Yes** | ||
| * **Type** – select the **RetrievedFile** entity | ||
| * **Variable Name** – enter `GetResponseFile` | ||
|
|
||
| 4. For images: | ||
| Drag a **Change object** action into the microflow after the **Call REST service** action, so that the image viewer will display the retrieved image. | ||
| * Double click the newly created action to open the properties. | ||
| * In the **Object** field, select the **GetResponseFile** variable for the **RetrievedFile** entity. | ||
| * In the **Refresh in client** field, select **Yes**. | ||
Binary file added
BIN
+138 KB
static/attachments/refguide/modeling/integration/rest-binary/no-export-mapping.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+6.25 KB
static/attachments/refguide/modeling/integration/rest-binary/starting-entity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.