I can successfully call a SOAP service using curl, but the same request sent via Apache CXF fails

6 days ago 9
ARTICLE AD BOX

Working curl request:

curl -v -X POST \
-H "Content-Type: text/xml;charset=UTF-8" \
-H "Authorization: Test <BASE64>" \
-H "SOAPAction: urn: CreateEmployeeByCode" \
--data '<soapenv:Envelope>...</soapenv:Envelope>' \
http://example.com/service

CXF request output (Not working)

Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[/], Authorization=[TEST <Different>], SOAPAction=[urn:CreateEmployeeByCode]}
Payload: <soapenv:Envelope>...</soapenv:Envelope>

@Component public class SoapHeaderInterceptor extends AbstractPhaseInterceptor {

public SoapHeaderInterceptor() { super(Phase.PRE_PROTOCOL); } @Override public void handleMessage(Message message) { Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS); if (headers == null) { headers = new HashMap<>(); message.put(Message.PROTOCOL_HEADERS, headers); } // Authorization (must match curl exactly) headers.put("Authorization", Collections.singletonList("Test <BASE64>")); // SOAPAction (note the space after urn:) headers.put("SOAPAction", Collections.singletonList("urn: CreateEmployeeByCode")); // Content-Type with charset headers.put("Content-Type", Collections.singletonList("text/xml;charset=UTF-8")); }

}

@Configuration public class EmployeeSoapConfig {

@Value("${employee.soap.endpoint}") private String endpointUrl; @Bean public EmployeeWebServicePortType employeePort(SoapHeaderInterceptor interceptor) { EmployeeWebService service = new EmployeeWebService(); EmployeeWebServicePortType port = service.getEmployeeWebServicePort(); Client client = ClientProxy.getClient(port); client.getEndpoint().getEndpointInfo().setAddress(endpointUrl); // attach interceptor client.getOutInterceptors().add(interceptor); return port; }

}

Read Entire Article