0
votes

I'm using HttpClient to send a PUT request with a body to Azure. The body is represented by a StringEntity.

I need to add the Azure authentication signature to the request, and in order to compute it correctly I need the values of the Content-Type and Content-Length headers.

When call the setEntity() method on the HttpPost request, no headers are added to the request, but using a HTTP debugging proxy I can see that they are correctly sent with the request.

From the Apache documentation (here) I saw I could use entity.getContentLength() and entity.getContentType() to compute those, but I would prefer to extract the data directly from the HttpPost if possible.

Anyone knows a way to force the entity to add the headers to the request, before the request is executed?

This is the code I'm using

HttpPut createBlob = new HttpPut(createBlobUrl);
createBlob.addHeader("x-ms-blob-type", "BlockBlob");
createBlob.addHeader("x-ms-date", utcTime);
createBlob.addHeader("x-ms-version", "2015-04-05");

HttpEntity body =  new StringEntity("test blob", "UTF-8");
createBlob.setEntity(body);

// h is missing Content-Length and Content-Type
Header[] h = createBlob.getAllHeaders();

resp = httpclient.execute(createBlob);
1

1 Answers

0
votes

I found a solution for it.

Adding an interceptor to the client, I can get the complete request with all the headers just before it's executed, so I don't need to deal with special cases where the headers are added by the entity.

HttpClientBuilder builder = HttpClientBuilder.create();
builder = builder.disableContentCompression().disableConnectionState();

builder.addInterceptorLast((HttpRequestInterceptor) (request, context) -> {
    try {
        SignRequest(request, account, secret);
     }
     catch (Exception e) {
     }
});

HttpClient httpclient = builder.build();

Official tutorial about interceptor can be found at this link