I would like to create an endpoint for uploading files with different Media/Content-Types (csv,jpg,xlsx... etc).
preferably using 1 flow.
In my main flow I would like to store the original Content-Type Header value and then replace it with "application/*" so the APIkit will route it to the postfile flow.But I can't get it to work.
This is what I have so far:
<flow name="test-main">
<http:listener config-ref="test-httpListenerConfig" path="/*">
<http:response statusCode="#vars.httpStatus default 200">
<http:headers><![CDATA[#vars.outboundHeaders default {}]]></http:headers>
</http:response>
<http:error-response statusCode="#vars.httpStatus default 500">
<http:body><![CDATA[#payload]]></http:body>
<http:headers><![CDATA[#vars.outboundHeaders default {}]]></http:headers>
</http:error-response>
</http:listener>
<set-variable value='#attributes.headers."content-type"' doc:name="Save Content-Type" variableName="headerContentType"/>
<ee:transform doc:name="Override Content-Type" >
<ee:message >
<ee:set-attributes ><![CDATA[%dw 2.0
import * from dw::util::Values
---
if (lower(attributes.method) == "post"
and
lower(attributes.headers."content-type") != "application/json"
)
// A file is POSTed, force the content-type to the correct API Kit Route
attributes update ["headers", field("content-type")] with "application/*"
//as Object {
// class: "org.mule.extension.http.api.HttpRequestAttributes"
//}
else
attributes
]]></ee:set-attributes>
</ee:message>
</ee:transform>
<apikit:router config-ref="test-config" />
</flow>
This is my test application
In Mule 3 I did this with a custom-transformer and a Java class, but that doesn't work anymore in Mule 4.
I hope someone here can help me with this problem.
I found a solution by building the whole attributes object from scratch
<ee:transform doc:name="Override Content-Type">
<ee:message >
<ee:set-attributes ><![CDATA[%dw 2.0
output application/java
fun updAttr(headers) =
headers update {
case ."content-type" -> "application/*"
}
---
if (lower(attributes.method) == "post"
and
lower(attributes.headers."content-type") != "application/json"
)
{
listenerPath: attributes.listenerPath,
rawRequestPath: attributes.rawRequestPath,
relativePath: attributes.relativePath,
version: attributes.version,
scheme: attributes.scheme,
method: attributes.method,
requestUri: attributes.requestUri,
rawRequestUri: attributes.rawRequestUri,
queryString: attributes.queryString,
localAddress: attributes.localAddress,
remoteAddress: attributes.remoteAddress,
queryParams: attributes.queryParams,
uriParams: attributes.uriParams,
requestPath: attributes.requestPath,
headers: updAttr(attributes.headers)
}
as Object {
class: "org.mule.extension.http.api.HttpRequestAttributes"
}
else
attributes]]></ee:set-attributes>
</ee:message>
<ee:variables >
</ee:variables>
</ee:transform>
I left out items:
- maskedRequestPath (resulted in "Invalid property name: `maskedRequestPath` on class `org.mule.extension.http.api.HttpRequestAttributesBuilder`")
- clientCertificate (resulted in "Errors while creating org.mule.runtime.api.util.SerializableLazyValue by reflection, even when class in on classpath")