Search code examples
apache-camelcontent-typespring-camel

Content-Type header does not reach the destination using Apache Camel


When executing the route, the content-type header does not reach the destination.

restConfiguration()
 .host("localhost")
 .port(8082)

from("seda:my-route")
  .setHeader(RestConstants.CONTENT_TYPE, constant("application/json"))
  .to("rest:get:/docp/supply-chain2/v1/invoice-responses")

I have tried to add the header from the route, using a processor and it does not work. Use apache camel constants and plain text. I have tried with the HTTP and REST component but neither has worked for me. Is there a way to force the sending of this header?


Solution

  • The GET method do not send the header "Content-Type" by default, You must have a body (different from null) and then use the getWithBody=true option to force it to be sent, so you can send the header:

    from("direct:start").routeId("hello")
        .setHeader(HttpConstants.CONTENT_TYPE, constant("application/json"))
        .setBody(constant("")) // use this and the Content-Type will be sent
        //.setBody(constant(null)) // use this and the Content-Type will not be sent
        .to("http://httpbin.org/anything?httpMethod=GET&getWithBody=true") // set getWithBody=false (the default) and the ContentType will not be sent
                                                                           // sorry if stating the obvious: if you use anything but GET, this is not needed
    .log("${body}");