Wiki source code of Multipart form-data

Last modified by Carlijn Kokkeler on 2024/09/03 12:30

Hide last authors
Erik Bakker 2.1 1 {{container}}{{container layoutStyle="columns"}}(((
Erik Bakker 32.1 2
3 {{info}}This microlearning is only applicable once running on the [[current generation architecture>>Main.eMagiz Academy.Fundamentals.fundamental-runtime-generation3||target="blank"]].{{/info}}
4
Carlijn Kokkeler 37.1 5 In this microlearning, we’ll dive into configuring multipart/form-data messages in eMagiz to ensure accurate delivery to an endpoint. You’ll learn the essentials of defining boundaries, setting content types, and constructing messages that comply with the HTTP standard. By understanding these key aspects, you’ll be able to efficiently handle complex data exchanges involving multiple parts and ensure seamless integration with external systems.
eMagiz 1.1 6
Erik Bakker 30.1 7 If you have any questions, please contact [[academy@emagiz.com>>mailto:academy@emagiz.com]].
eMagiz 1.1 8
9 == 1. Prerequisites ==
10
Erik Bakker 6.1 11 * Expert knowledge of the eMagiz platform
eMagiz 1.1 12
13 == 2. Key concepts ==
14
Erik Bakker 18.1 15 This microlearning focuses on configuring a multipart/form-data message within eMagiz to ensure it is sent correctly to an endpoint.
eMagiz 1.1 16
Erik Bakker 18.1 17 Key aspects are:
eMagiz 1.1 18
Erik Bakker 24.1 19 * Understanding the boundary that separates the parts of the message
Erik Bakker 18.1 20 * Defining the content types of each part of the message
21 * Construction of the complete message according to the specification
eMagiz 1.1 22
Erik Bakker 18.1 23 == 3. Multipart form-data ==
eMagiz 1.1 24
Erik Bakker 30.1 25 In this microlearning, we will focus on understanding how the multipart/form-data contentType works within the HTTP standard and which steps a user needs to take to construct such a message. Armed with this knowledge, you can implement these complex connections within a limited time frame.
Erik Bakker 24.1 26
27 === 3.1 Understanding the boundary ===
Erik Bakker 18.1 28
Erik Bakker 23.1 29 {{info}}
Erik Bakker 30.1 30 eMagiz will automatically generate a random boundary string that will change with each call. This way, you don't have to worry about this.
Erik Bakker 23.1 31 The following criteria apply when utilizing the boundary functionality within the multipart/form-data construction:
Erik Bakker 30.1 32 * The value of the boundary must begin with a double horizontal bar –this is called a leading hyphen
33 * The boundary value must not contain more than 70 characters in addition to the leading hyphen.
34 * The boundary value must not contain characters disabled by the HTTP protocol or URL, such as the colon.
35 * A CRLF line must always precede a boundary within the request body. This means that when the request body **ends** with a CRLF line, an **additional** CRLF line is needed **before** the boundary doubling the CRLF lines in that part of the request body.{{/info}}
Erik Bakker 23.1 36
Erik Bakker 24.1 37 === 3.3 Preparing the message ===
Erik Bakker 18.1 38
Erik Bakker 30.1 39 Several steps are needed to construct the message. Luckily, most of the steps necessary have to do with concepts we have already discussed in previous microlearnings. Based on what multipart/form-data entails, we need a way to store both the meta information and the file(s) we want to send to the external party. For example, put the metadata in one (or multiple) header(s) and use the file content as a payload. You can achieve this with a header enricher and standard transformer.
Erik Bakker 18.1 40
Erik Bakker 30.1 41 Once the file content is your payload, you must ensure the data is "raw." So, when you have a base64-encoded string, you should decode it before sending it to the endpoint.
Erik Bakker 18.1 42
Erik Bakker 30.1 43 In addition, we need to define the contentType header. Here, we need to define the value "multipart/form-data" to avoid potentially sending the incorrect contentType when calling the REST endpoint.
Erik Bakker 20.1 44
Erik Bakker 31.1 45 [[image:Main.Images.Microlearning.WebHome@expert-data-handling-multipart-form-data--content-type-header-config-new.png]]
Erik Bakker 19.1 46
Erik Bakker 20.1 47 === 3.2 Construct the message ===
Erik Bakker 19.1 48
Erik Bakker 20.1 49 After you have set the stage, you can use a standard transformer component to build your message correctly. To create it correctly, you need to take the following into account:
50
Erik Bakker 30.1 51 * Each part of the message needs to be a separate map entry. A comma separates these entries. The example below is the comma just before "'configuration'".
52 * Each value part (in the example below, the part after **file** and after **configuration** needs to be an HTTP entity object)
Erik Bakker 26.1 53 * Pay attention to the correct use of single quotes when constructing a message as part of this standard transformer.
Erik Bakker 30.1 54 * eMagiz will handle the boundary logic necessary to construct the message.
Erik Bakker 19.1 55
Erik Bakker 20.1 56 Given all this, you can write the following SpEL expression that will yield a desirable output:
Erik Bakker 19.1 57
Erik Bakker 29.1 58 {{code}}
Erik Bakker 28.1 59 {'file': new org.springframework.http.HttpEntity(payload, T(org.springframework.util.CollectionUtils).toMultiValueMap({'Content-Disposition': {'form-data; name="file"; filename="'+ headers.file_name + '"'}, 'Content-Type': 'text/csv'})),
Erik Bakker 26.1 60 'configuration': new org.springframework.http.HttpEntity('{"timestampFormat":"' + headers.timestampFormat + '",'name":"' + headers.customerId + '","workspace":{"id":"' + headers.workspaceId + '"}}'
Erik Bakker 30.1 61 , T(org.springframework.util.CollectionUtils).toMultiValueMap({'Content-Disposition': {'form-data; name="configuration"'}, 'Content-Type': 'application/json'}))}
Erik Bakker 28.1 62 {{/code}}
Erik Bakker 19.1 63
Erik Bakker 20.1 64 Putting this in a standard transformation gives you the following solution in the flow.
eMagiz 1.1 65
Erik Bakker 31.1 66 [[image:Main.Images.Microlearning.WebHome@expert-data-handling-multipart-form-data--standard-transformer-config-new.png]]
eMagiz 1.1 67
Erik Bakker 20.1 68 === 3.3 Calling the endpoint ===
eMagiz 1.1 69
Erik Bakker 30.1 70 Now that we have constructed our message correctly, the last thing to do is call the endpoint. Since we have prepared our message and accurately defined our contentType, calling the endpoint does not require additional configurations compared to what you are already used to when dealing with REST endpoints.
eMagiz 1.1 71
Eva Torken 22.1 72 == 4. Key takeaways ==
eMagiz 1.1 73
Erik Bakker 30.1 74 * eMagiz will automatically generate a random boundary string that will change with each call.
75 * Each part of the message needs to be a separate map entry. A comma separates these entries. The example below is the comma just before "'configuration'".
76 * Each value part (in the example below, the part after **file** and after **configuration** needs to be an HTTP entity object)
77 * Pay attention to the correct use of single quotes when constructing a message as part of this standard transformer.
78 * eMagiz will handle the boundary logic necessary to construct the message.
eMagiz 1.1 79
Eva Torken 22.1 80 == 5. Suggested Additional Readings ==
eMagiz 1.1 81
Carlijn Kokkeler 34.1 82 * [[Intermediate (Menu)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.WebHome||target="blank"]]
83 ** [[REST Connectivity (Navigation)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.REST Connectivity.WebHome||target="blank"]]
Carlijn Kokkeler 35.1 84 *** [[Configuration REST web service (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.REST Connectivity.intermediate-rest-webservice-connectivity-configuration-gen3.WebHome||target="blank"]]
Carlijn Kokkeler 34.1 85 *** [[Call a REST Webservice (Explanation)>>doc:Main.eMagiz Academy.Microlearnings.Intermediate Level.REST Connectivity.intermediate-rest-webservice-connectivity-call-a-rest-webservice||target="blank"]]
86 * [[Multipart form-data explained (External)>>https://www.sobyte.net/post/2021-12/learn-about-http-multipart-form-data/||target="blank"]]
Carlijn Kokkeler 36.1 87 * [[Multipart form data (Search Result)>>url:https://docs.emagiz.com/bin/view/Main/Search?sort=score&sortOrder=desc&highlight=true&facet=true&r=1&f_space_facet=0%2FMain.&f_type=DOCUMENT&f_locale=en&f_locale=&f_locale=en&text=mutipart+form+data||target="blank"]]
Eva Torken 22.1 88 )))((({{toc/}}))){{/container}}{{/container}}