httpclientaws-sdkjava-11aws-sdk-java-2.0aws-java-sdk-2.x

How to set the request body for POST request using the AwsCrtHttpClient (AWS CRT HTTP Client?)


I am creating HTTP requests with the AWS CRT HTTP Client, but I cannot find a way to set the request body. I am using Java 11 with the AWS SDK version 2.27.9. No matter how I configure this synchronous client, my requests do not have a request body.

I have tried to set the request body using the contentStreamProvider field on the SdkHttpFullRequest builder and the HttpExecuteRequest builder. Using both, I can provide a complete example:

SdkHttpClient httpClient = AwsCrtHttpClient.builder().build();
SdkHttpFullRequest sdkHttpRequest = SdkHttpFullRequest.builder()
        .uri(new URI("http://127.0.0.1:5000/post"))
        .contentStreamProvider(ContentStreamProvider.fromUtf8String("'{\"foo\":\"bar\"}'"))
        .method(SdkHttpMethod.POST)
        .appendHeader("accept", "application/json")
        .appendHeader("Content-Type", "application/json")
        .build();
HttpExecuteRequest request = HttpExecuteRequest.builder()
        .request(sdkHttpRequest)
        .contentStreamProvider(ContentStreamProvider.fromUtf8String("'{\"foo\":\"bar\"}'"))
        .build();
HttpExecuteResponse response = httpClient.prepareRequest(request).call();

I am running a local Python Flask app on port 5000 to view the request data, but the body of the request is always empty despite my efforts to set the request body. I have tried other servers, locally and remote, and they are not receiving data either.


Solution

  • As it turns out, the AWS CRT HTTP client is not really a full HTTP client to replace the built-in Java HTTP client. It's meant to be configured and then injected into other AWS service clients. We decided to continue using the standard Java HTTP client and drop the use of the AWS CRT HTTP client for our HTTP requests.