i transform a payload need to send as as multipart/form to request. the payload is like below
{
"goods_id": "12345,
"color_id_1": 20,
"image_id_1": vars.png1, //this is binary ,image data
"color_id_2": 11,
"image_id_2": vars.png1, //this is binary ,image data
}
here is sample file which we need to transform, the file is base on postman script. in postman we can choose image file from notebook. in payload, the image is an object which is stored in variable .
POST /cooperate/RegistAllImage HTTP/1.1
Content-Type: multipart/form-data; boundary=--------------------------549967118512544277394909
User-Agent: PostmanRuntime/7.29.0
Accept: */*
Cache-Control: no-cache
Postman-Token: ddddd
Host: ddddd
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: *****
Content-Length: 40980
----------------------------549967118512544277394909
Content-Disposition: form-data; name="goods_id"
Content-Type: application/x-www-form-urlencoded
12345
----------------------------549967118512544277394909
Content-Disposition: form-data; name="color_id_1"
Content-Type: application/x-www-form-urlencoded
20
----------------------------549967118512544277394909
Content-Disposition: form-data; name="image_1"; filename="064df7cefee0421990ccb74a5c1f9ccf.jpg"
Content-Type: application/octet-stream
<064df7cefee0421990ccb74a5c1f9ccf.jpg>
----------------------------549967118512544277394909
Content-Disposition: form-data; name="color_id_2"
Content-Type: application/x-www-form-urlencoded
2
----------------------------549967118512544277394909
Content-Disposition: form-data; name="image_2"; filename="064df7cefee0421990ccb74a5c1f9ccf.jpg"
Content-Type: application/octet-stream
<064df7cefee0421990ccb74a5c1f9ccf.jpg>
You can very well use the Multipart form module or can construct the parts using the script below.
%dw 2.0
import dw::module::Multipart
output multipart/form-data boundary="--------------------------549967118512544277394909"
---
{
parts: {
part1: Multipart::field({
name: "goods_id",
value: payload.goods_id,
mime: "appliation/x-www-form-urlencoded"
}),
part2: Multipart::field({
name: "color_id_1",
value: payload.color_id_1,
mime: "appliation/x-www-form-urlencoded"
})
,
part3: Multipart::field({
name: "image_id_1",
value: payload.image_id_1,
mime: "appliation/octet-stream",
fileName: "064df7cefee0421990ccb74a5c1f9ccf.jpg"
})
,
part4: Multipart::field({
name: "color_id_2",
value: payload.color_id_2,
mime: "appliation/x-www-form-urlencoded"
})
,
part5: Multipart::field({
name: "image_id_2",
value: payload.image_id_2,
mime: "appliation/octet-stream",
fileName: "064df7cefee0421990ccb74a5c1f9ccf.jpg"
})
}
}
Answer to the question in comment:
%dw 2.0
import dw::module::Multipart
output multipart/form-data boundary="--------------------------549967118512544277394909"
---
{
parts: payload mapObject ((value, key, index) -> {
("part" ++ ((index)+1)): Multipart::field({
"name": key as String,
"value": value as String,
"mime": if(key startsWith "image_id_") "application/octet-stream" else "application/x-www-form-urlencoded",
("fileName": "filenmae.png" )if(key startsWith "image_id_")
})
})
}