# Signing request body in Async HTTP Client

**URL:** https://community.gatling.io/t/signing-request-body-in-async-http-client/4416
**Category:** Gatling (Open-Source)
**Created:** [July 10, 2018, 6:58pm UTC](https://community.gatling.io/t/signing-request-body-in-async-http-client/4416 "2018-07-10T18:58:07Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Maciej\_Gawinecki](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Maciej\_Gawinecki](https://community.gatling.io/u/Maciej_Gawinecki)
#### Post date: [July 10, 2018, 6:58pm UTC](https://community.gatling.io/t/signing-request-body-in-async-http-client/4416/1 "2018-07-10T18:58:07Z")

</div>

Hi,

Our authentication algorithm calculates a signature of an HTTP request based on its body. The algorithm takes body as an array of bytes.

I have started to implement [`SignatureCalculator`](https://github.com/AsyncHttpClient/async-http-client/blob/master/client/src/main/java/org/asynchttpclient/SignatureCalculator.java), but found that Asynchronous HTTP Client (AHC) defines many different forms of body of [`Request`](https://github.com/AsyncHttpClient/async-http-client/blob/f723dc681f8aff9d8b858795632d4411cb515ff5/client/src/main/java/org/asynchttpclient/Request.java):

- `Request#getByteData`,
- `Request#getCompositeByteData`,
- `Request#getStringData`,
- `Request#getFile`,
- etc.

each requiring different way of serializing to `byte[]`. Is there any `serializeRequestBody(Request) : byte[]` method in Gatling or Asynchronous HTTP Client that covers different body types?

I have looked into existing `SignatureCalculator` implementations in AHC codebase, but all of them consider only one type of request body: `Request#getFormParams`:

- [OAuthSignatureCalculator](https://github.com/AsyncHttpClient/async-http-client/blob/f5663546db0a69d316b2b13995ee4f103efef00c/client/src/main/java/org/asynchttpclient/oauth/OAuthSignatureCalculator.java)
- [StaticOAuthSignatureCalculator](https://github.com/AsyncHttpClient/async-http-client/blob/f5663546db0a69d316b2b13995ee4f103efef00c/client/src/test/java/org/asynchttpclient/oauth/StaticOAuthSignatureCalculator.java)

My implementation so far:

private byte[] serializeRequestBody(Request request) {

if (request.getByteData() != null) {  
return request.getByteData();  
} else if (request.getCompositeByteData() != null) {  
List buff = new ArrayList\<\>();  
for (byte[] bytes : request.getCompositeByteData()) {  
buff.addAll(Bytes.asList(bytes));  
}  
return Bytes.toArray(buff);  
} else if (request.getStringData() != null) {  
throw new UnsupportedOperationException(“Serializing StringData in request body is not supported”);  
} else if (request.getByteBufferData() != null) {  
throw new UnsupportedOperationException(“Serializing ByteBufferData in request body is not supported”);  
} else if (request.getStreamData() != null) {  
throw new UnsupportedOperationException(“Serializing StreamData in request body is not supported”);  
} else if (isNonEmpty(request.getFormParams())) {  
throw new UnsupportedOperationException(“Serializing FormParams in request body is not supported”);  
} else if (isNonEmpty(request.getBodyParts())) {  
throw new UnsupportedOperationException(“Serializing BodyParts in request body is not supported”);  
} else if (request.getFile() != null) {  
throw new UnsupportedOperationException(“Serializing File in request body is not supported”);  
} else if (request.getBodyGenerator() instanceof FileBodyGenerator) {  
throw new UnsupportedOperationException(“Serializing FileBodyGenerator in request body is not supported”);  
} else if (request.getBodyGenerator() instanceof InputStreamBodyGenerator) {  
throw new UnsupportedOperationException(“Serializing InputStreamBodyGenerator in request body is not supported”);  
} else if (request.getBodyGenerator() instanceof ReactiveStreamsBodyGenerator) {  
throw new UnsupportedOperationException(“Serializing ReactiveStreamsBodyGenerator in request body is not supported”);  
} else if (request.getBodyGenerator() != null) {  
throw new UnsupportedOperationException(“Serializing generic BodyGenerator in request body is not supported”);  
} else {  
return new byte[]{};  
}  
}
