How to set HTTP header with javaagent

I’m using a Java lib which internally uses Apache HttpClient 4.3 for sending https requests. The 3rd party server requires the ‘Content-Type’ header which is unfortunately not set by the lib.

As changing the lib is not an option, I’d like to use javaagents to add the header.

I found this useful tutorial which made me believe it’s possible to achieve this: But I couldn’t figure out which interface of HttpClient 4.3 to manipulate to set the header. Does anybody know how it could work?


The solution I came up with: using bytebuddy to intercept the ‘doExecute’ Method of the Apache InternalHttpClient that is used by the 3rd party library. So I was able to add the required content-type header.

public class AgentMain {

    public static void premain(String agentArgs, Instrumentation inst) {
        new AgentBuilder.Default()
                .transform((builder, type, classLoader, module) ->

    public static void agentmain(String agentArgs, Instrumentation inst) {
        // Not used

    public static class HttpClientAdvice {
        public static void doExecute(@Advice.AllArguments Object[] args) {
            final HttpRequest request = (HttpRequest) args[1];
            request.addHeader("Content-Type", "text/xml");