title: Sending PDF over HTTP Request in Mule 4
body: <p>I have in my flow an HTTP request (say, DOC Service) where I get a PDF from a service, and I have to send the PDF to an OCR application through another HTTP Request (say, OCR Service). I am receiving the PDF from DOC Service as application/pdf type, and the OCR Service is set to receive multipart/form-data type.</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;mule xmlns:ftp=&quot;http://www.mulesoft.org/schema/mule/ftp&quot; xmlns:file=&quot;http://www.mulesoft.org/schema/mule/file&quot;
      xmlns:http=&quot;http://www.mulesoft.org/schema/mule/http&quot;
      xmlns:ee=&quot;http://www.mulesoft.org/schema/mule/ee/core&quot; xmlns=&quot;http://www.mulesoft.org/schema/mule/core&quot; xmlns:doc=&quot;http://www.mulesoft.org/schema/mule/documentation&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
  http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
  http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd&quot;&gt;
      &lt;flow name=&quot;on-mq-messageFlow&quot; doc:id=&quot;eca15aeb-da46-491a-97e1-f73aa5fcd45b&quot; &gt;
          &lt;set-payload value='#[%dw 2.0
  output application/json
  ---
  read(payload,&quot;application/json&quot;)]' doc:name=&quot;Set Payload&quot; doc:id=&quot;929f7b70-ae47-4fe3-abd2-e89ae092899e&quot; /&gt;
          &lt;set-variable value=&quot;#[payload]&quot; doc:name=&quot;Set MQ Payload&quot; doc:id=&quot;9716ed4b-4d16-4b9e-8ac8-1dab5646fe0f&quot; variableName=&quot;mqPayload&quot;/&gt;
          &lt;http:request method=&quot;POST&quot; doc:name=&quot;Doc Request&quot; doc:id=&quot;0e92add3-d576-444d-8070-9d09e6e3c5c5&quot; config-ref=&quot;DOC_configuration&quot; path=&quot;${doc.request.path}&quot; outputMimeType='multipart/form-data; boundary=----aA1bB2cC3dD4'&gt;
              &lt;http:headers &gt;&lt;![CDATA[#[output application/java
  ---
  {
      &quot;client-id&quot; : &quot;12345&quot;,
      &quot;client-secret&quot; : &quot;12345&quot;
  }]]]&gt;&lt;/http:headers&gt;
              &lt;http:uri-params &gt;&lt;![CDATA[#[output application/java
  ---
  {
      &quot;doc_id&quot; : payload.data.ID
  }]]]&gt;&lt;/http:uri-params&gt;
  
          &lt;/http:request&gt;
          &lt;http:request method=&quot;POST&quot; doc:name=&quot;Send Doc to OCR&quot; doc:id=&quot;76f60ddb-f570-42be-b85e-ccb2d2ae3703&quot; config-ref=&quot;OCR_Request_configuration&quot; path=&quot;${ocr.request.path}&quot; outputMimeType=&quot;multipart/form-data; boundary=----aA1bB2cC3dD4&quot;&gt;
              &lt;http:body &gt;&lt;![CDATA[#[%dw 2.0
  output multipart/form-data
  ---
  {
      parts: {
          file: {
              headers: {
                  &quot;Content-Type&quot;: payload.^mimeType
              },
              content: payload
          },
          id: {
              headers: {
                  &quot;Content-Type&quot;: &quot;text/plain&quot;
              },
              content: vars.mqPayload.data.ID
          }
      }
  }]]]&gt;&lt;/http:body&gt;
              &lt;http:headers &gt;&lt;![CDATA[#[output application/java
  ---
  {
      &quot;client-id&quot; : &quot;12345&quot;,
      &quot;Accept&quot; : &quot;*/*&quot;,
      &quot;client-secret&quot; : &quot;12345&quot;,
      &quot;Content-Type&quot; : &quot;multipart/form-data; boundary=----aA1bB2cC3dD4&quot;
  }]]]&gt;&lt;/http:headers&gt;
          &lt;/http:request&gt;
          &lt;ee:transform doc:name=&quot;Final Response&quot; doc:id=&quot;cf68dbc1-cee3-4809-9511-a0794be2b5f7&quot;&gt;
              &lt;ee:message&gt;
                  &lt;ee:set-payload&gt;&lt;![CDATA[%dw 2.0
  output application/json
  ---
  {
      &quot;status&quot;: &quot;Success&quot;,
      message: &quot;Completed&quot;
  }
  ]]&gt;&lt;/ee:set-payload&gt;
              &lt;/ee:message&gt;
          &lt;/ee:transform&gt;
      &lt;/flow&gt;
  &lt;/mule&gt;
  </code></pre>
  <p>I have to add another text field along with the PDF file before sending the payload to OCR Service. For that, I have defined payload as multipart/form-data by selecting the elements of parts of form-data and modifying it like given in the answer <a href="https://stackoverflow.com/questions/58475652/how-to-send-a-file-using-http-request-connector-in-mule4">here</a>, but getting below error. I have also set MimeType for both DOC Service and OCR Service as multipart/form-data.</p>
  <pre><code>ERROR 2020-12-05 21:57:57,159 [[MuleRuntime].cpuLight.02: [doc-process-api].on-mq-messageFlow.CPU_LITE @6ec62e93] [event: 0e82ca00-3717-11eb-ab69-005056ac6a26] org.mule.runtime.core.internal.exception.OnErrorPropagateHandler: 
  ********************************************************************************
  Message               : &quot;javax.mail.internet.ParseException - Missing start boundary, while reading `payload` as MultiPart.
  Trace:
    at main (Unknown), while writing MultiPart.
  Trace:
    at main (Unknown)&quot; evaluating expression: &quot;%dw 2.0
  output multipart/form-data
  ---
  {
  parts: {
  file: {
  headers: {
  &quot;Content-Type&quot;: payload.^mimeType
  },
  content: payload
  },
  external_id: {
  headers: {
  &quot;Content-Type&quot;: &quot;text/plain&quot;
  },
  content: vars.mqPayload.data.ID
  }
  }
  }&quot;.
  Element               : on-mq-messageFlow/processors/4 @ doc-process-api:on-mq-message.xml:46 (Send Doc to OCR)
  Element XML           : &lt;http:request method=&quot;POST&quot; doc:name=&quot;Send Doc to OCR&quot; doc:id=&quot;76f60ddb-f570-42be-b85e-ccb2d2ae3703&quot; config-ref=&quot;OCR_Request_configuration&quot; path=&quot;${ocr.request.path}&quot; outputMimeType=&quot;multipart/form-data; boundary=----aA1bB2cC3dD4&quot;&gt;
                          &lt;http:body&gt;#[%dw 2.0
  output multipart/form-data
  ---
  {
      parts: {
          file: {
              headers: {
                  &quot;Content-Type&quot;: payload.^mimeType
              },
              content: payload
          },
          external_id: {
              headers: {
                  &quot;Content-Type&quot;: &quot;text/plain&quot;
              },
              content: vars.mqPayload.data.ID
          }
      }
  }]&lt;/http:body&gt;
                          &lt;http:headers&gt;#[output application/java
  ---
  {
      &quot;client-id&quot; : &quot;12345&quot;,
      &quot;Accept&quot; : &quot;*/*&quot;,
      &quot;client-secret&quot; : &quot;12345&quot;,
      &quot;Content-Type&quot; : &quot;multipart/form-data; boundary=----aA1bB2cC3dD4&quot;
  }]&lt;/http:headers&gt;
                          &lt;/http:request&gt;
  Error type            : MULE:EXPRESSION
  Payload Type          : org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider
  --------------------------------------------------------------------------------
  Root Exception stack trace:
  org.mule.runtime.api.el.ExpressionExecutionException: javax.mail.internet.ParseException - Missing start boundary, while reading `payload` as MultiPart.
  Trace:
    at main (Unknown), while writing MultiPart.
  Trace:
    at main (Unknown)
      at org.mule.weave.v2.el.WeaveExpressionLanguageSession.doEvaluate(WeaveExpressionLanguageSession.scala:130)
      at org.mule.weave.v2.el.WeaveExpressionLanguageSession.evaluate(WeaveExpressionLanguageSession.scala:77)
      at org.mule.weave.v2.el.WeaveExpressionLanguageSession.evaluate(WeaveExpressionLanguageSession.scala:103)
      at org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor$1.evaluate(DataWeaveExpressionLanguageAdaptor.java:252)
      at org.mule.runtime.core.internal.el.DefaultExpressionManagerSession.evaluate(DefaultExpressionManagerSession.java:38)
      at org.mule.runtime.core.privileged.util.attribute.ExpressionAttributeEvaluatorDelegate.resolveExpressionWithSession(ExpressionAttributeEvaluatorDelegate.java:70)
      at org.mule.runtime.core.privileged.util.attribute.ExpressionAttributeEvaluatorDelegate.resolve(ExpressionAttributeEvaluatorDelegate.java:56)
      at org.mule.runtime.core.privileged.util.AttributeEvaluator.resolveTypedValue(AttributeEvaluator.java:104)
      at org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionValueResolver.resolveTypedValue(ExpressionValueResolver.java:122)
      at org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionTypedValueValueResolver.resolve(ExpressionTypedValueValueResolver.java:46)
      at org.mule.runtime.module.extension.internal.runtime.resolver.ExpressionTypedValueValueResolver.resolve(ExpressionTypedValueValueResolver.java:29)
      at org.mule.runtime.module.extension.internal.runtime.resolver.ResolverUtils.resolveRecursively(ResolverUtils.java:90)
      at org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet.resolve(ResolverSet.java:109)
      at org.mule.runtime.module.extension.internal.runtime.operation.ComponentMessageProcessor.getResolutionResult(ComponentMessageProcessor.java:586)
      at org.mule.runtime.module.extension.internal.runtime.operation.ComponentMessageProcessor.lambda$apply$6(ComponentMessageProcessor.java:194)
      at org.mule.runtime.core.api.util.func.CheckedFunction.apply(CheckedFunction.java:19)
      at org.mule.runtime.core.api.rx.Exceptions.lambda$checkedFunction$2(Exceptions.java:84)
      at reactor.core.publisher.FluxFlatMap$FlatMapMain.onNext(FluxFlatMap.java:381)
      at reactor.core.publisher.FluxFlatMap$FlatMapMain.tryEmit(FluxFlatMap.java:532)
      at reactor.core.publisher.FluxFlatMap$FlatMapInner.onNext(FluxFlatMap.java:974)
      at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:121)
      at reactor.core.publisher.Operators$ScalarSubscription.request(Operators.java:2071)
      at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.request(FluxMapFuseable.java:162)
      at reactor.core.publisher.FluxFlatMap$FlatMapInner.onSubscribe(FluxFlatMap.java:964)
      at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onSubscribe(FluxMapFuseable.java:90)
      at reactor.core.publisher.MonoCurrentContext.subscribe(MonoCurrentContext.java:35)
      at reactor.core.publisher.MonoMapFuseable.subscribe(MonoMapFuseable.java:59)
      at reactor.core.publisher.Mono.subscribe(Mono.java:3858)
      at reactor.core.publisher.FluxFlatMap$FlatMapMain.onNext(FluxFlatMap.java:420)
      at org.mule.runtime.core.privileged.processor.chain.AbstractMessageProcessorChain$1.onNext(AbstractMessageProcessorChain.java:292)
      at org.mule.runtime.core.privileged.processor.chain.AbstractMessageProcessorChain$1.onNext(AbstractMessageProcessorChain.java:285)
      at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:204)
      at reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onNext(FluxOnAssembly.java:345)
      at reactor.core.publisher.FluxPeekFuseable$PeekFuseableSubscriber.onNext(FluxPeekFuseable.java:204)
      at reactor.core.publisher.FluxContextStart$ContextStartSubscriber.tryOnNext(FluxContextStart.java:111)
      at reactor.core.publisher.FluxContextStart$ContextStartSubscriber.tryOnNext(FluxContextStart.java:109)
      at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.tryOnNext(FluxMapFuseable.java:303)
      at reactor.core.publisher.FluxContextStart$ContextStartSubscriber.tryOnNext(FluxContextStart.java:109)
      at reactor.core.publisher.FluxPublishOn$PublishOnConditionalSubscriber.runAsync(FluxPublishOn.java:866)
      at reactor.core.publisher.FluxPublishOn$PublishOnConditionalSubscriber.run(FluxPublishOn.java:939)
      at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84)
      at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at org.mule.service.scheduler.internal.AbstractRunnableFutureDecorator.doRun(AbstractRunnableFutureDecorator.java:111)
      at org.mule.service.scheduler.internal.RunnableFutureDecorator.run(RunnableFutureDecorator.java:54)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  
  </code></pre>
  <p>To fix that I tried adding boundary in headers as Content-Type like below, and still getting the same error.</p>
  <pre><code>&quot;Content-Type: &quot;multipart/form-data; boundary=----aA1bB2cC3dD4&quot;
  </code></pre>
  <p>Here I don't need to parse anything, just handover the PDF from one service to another service, but by adding an extra field. How can I achieve this? Any help is appreciated.</p>