How to set request body with AWS CRT HTTP Client?

0

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 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 with 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.

asked a month ago45 views
2 Answers
0
Accepted Answer

It seems like 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.

answered a month ago
0

The issue you're encountering is related to how the request body is being handled by the AWS CRT HTTP client. In AWS SDK for Java v2, the SdkHttpFullRequest builder and HttpExecuteRequest builder both have a method to set the request body using ContentStreamProvider. However, it seems like the AWS CRT HTTP client is not correctly passing the request body.

To solve this, ensure that the ContentStreamProvider is correctly associated with the request and that the body is not being overwritten or ignored by the client configuration

profile pictureAWS
EXPERT
Deeksha
answered a month ago
  • Thank you for your response. The ContentStreamProvider seems properly associated with the request. I can set a breakpoint near the point of the execution of the request, and I see at the line HttpStreamBase stream = httpClientConnectionMakeRequest(this.getNativeHandle(), request.marshalForJni(), request.getBodyStream(), new HttpStreamResponseHandlerNativeAdapter(streamHandler)); that the request.getBodyStream() value contains the data I would like to include. The is line #50 within the makeRequest() method within the HttpClientConnection.java file. I am setting ContentStreamProvider with the HttpExecuteRequest builder.

    Perhaps I lack an understanding of the low-level implementation of this client. I am expecting my server to receive the data within the request's message body per RFC 7230. Do my servers need to handle the message body differently than a normal POST request? "Normally", this data is accessible within the request's data field in Flask or also the data attribute via cURL as described here. I can post the data to my test servers using cURL, but the servers are not receiving the data if I use this AWS CRT HTTP Client.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions