kash-hash
kash-hash

Reputation: 13

How to Access Request Body in Armeria Client Decorator

I need to intercept and sign my client requests in Armeria using AWS v4 signature. That signing procedure needs access to the body of the request, but it seems like I can't access request body in the client decorator. Something like

wcb.decorator((delegate, ctx, req) -> {
                 // constructAWS4SignHeader needs the body of req.
                 String auth = constructAWS4SignHeader(req);
                 HttpRequest newReq = req.withHeaders(req.headers().toBuilder().set(AUTHORIZATION, auth));
                 ctx.updateRequest(newReq);
                 return delegate.execute(ctx, req);
            });

Any solution comes to mind?

Upvotes: 0

Views: 341

Answers (1)

minwoox
minwoox

Reputation: 111

in the current version which is Armeria 1.19.0, I guess we can do:

wcb.decorator((delegate, ctx, req) -> {
    final CompletableFuture<HttpResponse> future = new CompletableFuture<>();
    req.aggregate().handle((aggregatedReq, cause) -> {
        try {
            // constructAWS4SignHeader needs the body of req.
            String auth = constructAWS4SignHeader(aggregatedReq);
            final RequestHeaders newHeaders = req.headers()
                                                 .toBuilder()
                                                 .set(AUTHORIZATION, auth)
                                                 .build();
            final HttpRequest newReq = HttpRequest.of(newHeaders, aggregatedReq.content(),
                                                      aggregatedReq.trailers());
            ctx.updateRequest(newReq);
            future.complete(delegate.execute(ctx, newReq));
        } catch (Exception e) {
            future.completeExceptionally(e);
        }
        return null;
    });
    return HttpResponse.from(future);
})

Upvotes: 1

Related Questions