1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Have you also upgraded to Camel 3.x? If you are using Camel 2.x, then you still need `http4` dependencies, we have not released website for older versions yet. For documentation of `2.x` see adoc files in relevant branch on github. E.g. here is documentation for `2.24.x` https://github.com/apache/camel/blob/camel-2.24.x/components/camel-http4/src/main/docs/http4-component.adoc . TLDR in Camel 2 use `camel-http4-starter` instead `camel-http-starter` – Bedla Sep 02 '19 at 18:51

0 Answers0