In Connect a Connectors class exists which automatically detectsevery connector in the classpath. It can be used to get the HTTP connectorinstance by its connector ID, which is http-connector.

  1. HttpConnector http = Connectors.getConnector(HttpConnector.ID);

Configure Apache HTTP Client

Camunda Connect HTTP client uses the Apache HTTP client to make HTTP requests. Accordingly, it supports the same configuration options.

Default Configuration

By default, the HTTP client uses Apache’s default configuration and respects the system properties that are supported by HTTP client.

Custom Configuration

If you want to reconfigure the client going beyond the default configuration options, e.g. you want to configure another connection manager, the easiest way is to registera new connector configurator.

  1. package org.camunda.connect.example;
  2. import org.apache.http.impl.client.CloseableHttpClient;
  3. import org.apache.http.impl.client.HttpClients;
  4. import org.camunda.connect.httpclient.impl.AbstractHttpConnector;
  5. import org.camunda.connect.spi.ConnectorConfigurator;
  6. public class HttpConnectorConfigurator implements ConnectorConfigurator<HttpConnector> {
  7. public Class<HttpConnector> getConnectorClass() {
  8. return HttpConnector.class;
  9. }
  10. public void configure(HttpConnector connector) {
  11. CloseableHttpClient client = HttpClients.custom()
  12. .setMaxConnPerRoute(10)
  13. .setMaxConnTotal(200)
  14. .build();
  15. ((AbstractHttpConnector) connector).setHttpClient(client);
  16. }
  17. }

To enable auto detection of your new configurator please add a file calledorg.camunda.bpm.connect.spi.ConnectorConfigurator to yourresources/META-INF/services directory with class name as content. For moreinformation see the extending Connect section.

  1. org.camunda.connect.example.HttpConnectorConfigurator

Requests

Create a Simple HTTP Request

The HTTP connector can be used to create a new request, set a HTTP method, URL,content type and payload.

A simple GET request:

  1. http.createRequest()
  2. .get()
  3. .url("http://camunda.org")
  4. .execute();

A POST request with a content type and payload set:

  1. http.createRequest()
  2. .post()
  3. .url("http://camunda.org")
  4. .contentType("text/plain")
  5. .payload("Hello World!")
  6. .execute();

The HTTP methods PUT, DELETE, PATCH, HEAD, OPTIONS, TRACEare also available.

Adding HTTP Headers to a Request

To add own headers to the HTTP request the method header isavailable.

  1. HttpResponse response = http.createRequest()
  2. .get()
  3. .header("Accept", "application/json")
  4. .url("http://camunda.org")
  5. .execute();

Using the Generic API

Besides the configuration methods also a generic API exists toset parameters of a request. The following parameters areavailable:

Parameter Description
method Sets the HTTP method of the request
url Sets the URL of the request
headers Contains a map of the configured HTTP headers of the request
payload Sets the payload of the request

This can be used as follows:

  1. HttpRequest request = http.createRequest();
  2. request.setRequestParameter("method", "GET");
  3. request.setRequestParameter("url", "http://camunda.org");
  4. request.setRequestParameter("payload", "hello world!");

Response

A response contains the status code, response headers and body.

  1. Integer statusCode = response.getStatusCode();
  2. String contentTypeHeader = response.getHeader("Content-Type");
  3. String body = response.getResponse();

After the response was processed it should be closed.

  1. response.close()

Using the Generic API

Besides the response methods a generic API is providedto gather the response parameters. The following parametersare available:

Parameter Description
statusCode Contains the status code of the response
headers Contains a map with the HTTP headers of the response
response Contains the response body

This can be used as follows:

  1. response.getResponseParameter("statusCode");
  2. response.getResponseParameter("headers");
  3. response.getResponseParameter("response");

原文: https://docs.camunda.org/manual/7.9/reference/connect/http-connector/