Adding HTTP Headers to a SOAP Request in Java

Salu Khadka
1 min readOct 4, 2020

With an intelligent IDE, one can easily generate Java Code from the WDSL file. However, some SOAP methods require additional security header parameters to function.

curl --location --request POST '<url>' \
--header 'header_key: header_value' \
--header 'Content-Type: text/xml' \
--data-raw '<soapenv:Envelopexmlns:soapenv="" xmlns:ws=”" xmlns:xsd=”http://test/xsd"><soapenv:Header/>
<soapenv:Body>... </soapenv:Body>

The generated service does not include space for such headers and we might land into trouble on how to pass the header.

ExampleService service = new ExampleService();
IExampleService client = service.getWSHttpBindingIExampleService();
client.soapMethod(param1, param2);

However, we can pass the map directly to the request context of the service.

Map<String, List<String>> requestHeaders = new HashMap<>();
requestHeaders.put("header_key", Collections.singletonList(value));
((BindingProvider) client)
.put(MessageContext.HTTP_REQUEST_HEADERS, getHeader(apiKey));
client.soapMethod(param1, param2);

This way, the soap method will contain the HTTP header we want to pass.