How to post form-data in ServiceTask to remote REST Service

Dear Community,

in my project I would like to embed calling a remote REST-Service (implemented using Spring) into my Camunda process and pass some form-data to the remote service as well. The process definintion is as follows:

...
<bpmn2:process id="emotion-metadata" name="Emotion Metadata" isExecutable="true">
    <bpmn2:startEvent id="StartEvent_1">
      <bpmn2:outgoing>SequenceFlow_1</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:serviceTask id="ServiceTask_1" name="Some REST Call">
      <bpmn2:extensionElements>
        <camunda:connector>
          <camunda:inputOutput>
            <camunda:inputParameter name="url">http://localhost:8085/some-service/some-endpoint</camunda:inputParameter>
            <camunda:inputParameter name="method">POST</camunda:inputParameter>
            <camunda:inputParameter name="form-data">
              <camunda:map>
                <camunda:entry key="some-param-one">somevalue</camunda:entry>
                <camunda:entry key="some-param-two">someothervalue</camunda:entry>
              </camunda:map>
            </camunda:inputParameter>
          </camunda:inputOutput>
          <camunda:connectorId>http-connector</camunda:connectorId>
        </camunda:connector>
      </bpmn2:extensionElements>
      <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_02afwp4</bpmn2:outgoing>
    </bpmn2:serviceTask>
    <bpmn2:sequenceFlow id="SequenceFlow_1" name="" sourceRef="StartEvent_1" targetRef="ServiceTask_1" />
    <bpmn2:endEvent id="EndEvent_2">
      <bpmn2:incoming>SequenceFlow_02afwp4</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_02afwp4" sourceRef="ServiceTask_1" targetRef="EndEvent_2" />
  </bpmn2:process>
...

If I check the execution with debugger, I can see that the form-data parameters are set in the request as a simple request parameter - but sadly this is not the way the remote service expects them.
I think, since Camunda’s HTTP connector is based on Apache HTTP Client (HTTP Connector | docs.camunda.org), these parameters should be mapped by for example by

 org.apache.http.client.entity.UrlEncodedFormEntity

Is there a way to use a custom mapping so that the variables can be posted to the remote service as form-data? Should a custom configuration, a custom connector or just a custom mapping be used?

Thank you in advance!

Hello, @d_bence.
You can configure a java class to your service task, and implement a call to your rest client by FeignClient or Retrofit.
For me it’s better than use http-connector…

1 Like

Couldn’t agree more

Hi @regissantana,

thank you for your reply. I know that I could write a Java class for dealing with the problem, but then I’d have to implement the whole service call myself. I hoped that there is an other way to do it, since the framework already supports REST-calls (via the http-connector).

Ok @d_bence , but don’t be afraid, look at this simple rest client:

@FeignClient(name = "clientTest", url = "${my.client.url}")
public interface ITestClient {

    @PostMapping(value = "create")
    ResponseEntity<ProductDto> createProduct(@RequestBody ProductDto productDto);

}

This will do:

POST myUrl/create
body:
{
"productName": "Car",
"description": "Beautiful car"
}

Improve this with your HEADERS etc.
Hope it helps.

1 Like

Thank you, implementing my custom Java class task solved the problem.