HTTP Connector
In Camunda Connect a Connectors
class exists which automatically detects
every connector in the classpath. It can be used to get the HTTP connector
instance by its connector ID, which is http-connector
.
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 register a new connector configurator.
package org.cibseven.connect.example;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.cibseven.connect.httpclient.impl.AbstractHttpConnector;
import org.cibseven.connect.spi.ConnectorConfigurator;
public class HttpConnectorConfigurator implements ConnectorConfigurator<HttpConnector> {
public Class<HttpConnector> getConnectorClass() {
return HttpConnector.class;
}
public void configure(HttpConnector connector) {
CloseableHttpClient client = HttpClients.custom()
.setMaxConnPerRoute(10)
.setMaxConnTotal(200)
.build();
((AbstractHttpConnector) connector).setHttpClient(client);
}
}
To enable auto detection of your new configurator please add a file called
org.cibseven.connect.spi.ConnectorConfigurator
to your
resources/META-INF/services
directory with class name as content. For more
information see the extending Connect section.
org.cibseven.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:
http.createRequest()
.get()
.url("http://camunda.org")
.execute();
A POST request with a content type and payload set:
http.createRequest()
.post()
.url("http://camunda.org")
.contentType("text/plain")
.payload("Hello World!")
.execute();
The HTTP methods PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE are also available.
Adding HTTP Headers to a Request
To add own headers to the HTTP request the method header
is
available.
HttpResponse response = http.createRequest()
.get()
.header("Accept", "application/json")
.url("http://camunda.org")
.execute();
Enabling HTTP Response Error Handling
By default, the HTTP connector does not seamlessly handle 4XX and 5XX related response errors during HTTP call.
To activate the handling of these errors without additional scripting, set the throw-http-error
property to TRUE
via the configOption
method. Once enabled, the client will throw an exception in case of http response errors (status code 400-599).
HttpResponse response = http.createRequest()
.get()
.configOption("throw-http-error", "TRUE")
.url("http://camunda.org")
.execute();
Using the Generic API
Besides the configuration methods also a generic API exists to set parameters of a request. The following parameters are available:
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:
HttpRequest request = http.createRequest();
request.setRequestParameter("method", "GET");
request.setRequestParameter("url", "http://camunda.org");
request.setRequestParameter("payload", "hello world!");
Response
A response contains the status code, response headers and body.
Integer statusCode = response.getStatusCode();
String contentTypeHeader = response.getHeader("Content-Type");
String body = response.getResponse();
After the response was processed it should be closed.
response.close()
Using the Generic API
Besides the response methods a generic API is provided to gather the response parameters. The following parameters are 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:
response.getResponseParameter("statusCode");
response.getResponseParameter("headers");
response.getResponseParameter("response");