Andreas Warberg
Andreas Warberg

Reputation: 640

API Management <forward-request> fails with error message "Cannot access a closed Stream."

As part of a requirement to inspect to request body of our API Management proxy requests, I have added a call to context.Request.Body.As<string>(preserveContent: true). However, after this change, my <forward-request> policies fail with Cannot access a closed Stream.

The error message does not hold many clues, but I think the stream, it refers to, is the inbound request body. For this reason I used the preserveContent: true overload, but it fails regardless.

What are some other sources that may cause the stream to become closed and how can I keep it open, so that <forward-request> can forward the request body as well?

Upvotes: 1

Views: 808

Answers (1)

Andreas Warberg
Andreas Warberg

Reputation: 640

I solved this issue by inserting a <set-body> policy in the end of my <inbound> section like this: <set-body>@(context.Variables.GetValueOrDefault<string>("Request.Body"))</set-body>.

Upvotes: 3

Related Questions