Reputation: 11
I have a Spring Boot app that uses Camel to POST a multipart/form-data request to a REST endpoint. The request includes a text part and a file part. The code that builds the request is as follows:
@Override
public void process(Exchange exchange) throws Exception {
@SuppressWarnings("unchecked")
Map<String, String> body = exchange.getIn().getBody(Map.class);
String fileName = body.get("FILE_NAME");
String filePath = body.get("FILE_PATH");
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addTextBody("name", fileName, ContentType.DEFAULT_TEXT);
entity.addBinaryBody("file", new File(filePath),
ContentType.APPLICATION_OCTET_STREAM, fileName);
exchange.getIn().setBody(entity.build());
}
.to("https4://<endpoint>")
This code works nicely. In my pom.xml file I am importing the camel-http4 component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http4</artifactId>
<version>${camel.version}</version>
</dependency>
I have tried replacing the camel-http4 component with camel-http-starter, as suggested by the latest Camel documentation at https://camel.apache.org/components/latest/http-component.html
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http-starter</artifactId>
<version>${camel.version}</version>
</dependency>
and replace all http4 endpoints with http, but the code above now fails because there is no type converter available between HttpEntity and InputStream in the camel-http component.
I have tried using Camel's MIME Multipart DataFormat:
.setHeader("name", simple("${body[FILE_NAME]}"))
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
@SuppressWarnings("unchecked")
Map<String, String> body = exchange.getIn().getBody(Map.class);
String filePath = body.get("FILE_PATH");
exchange.getIn().setBody(new File(filePath));
}
})
// Add only "name" header in multipart request
.marshal().mimeMultipart("form-data", true, true, "(name)", true)
.to("https://<endpoint>")
But I keep getting HTTP 400 errors from the server, meaning it doesn't understand the request. So my question is: How to use the MIME Multipart DataFormat (or any other way) to obtain the same multipart request as the previous working code?
Upvotes: 1
Views: 1171