Azure APIM - Outbound Policy - Set Outbound Body

juni dev 361 Reputation points
2022-12-08T23:46:00.447+00:00

Hi,

I have an API Operation calling a service that returns only 204.
I need to return in the response to the client a variable that was generated in APIM inbound policy and used on the request to the backend

I need to return to the client the value of that variable.
I'm able to set in the in the response header but not in the Body (the response body continues to be empty).

How can I return the variable in the outbound body?

I have it working for the header like this:

<outbound>
<base />
<set-header name="myHeaderName" exists-action="override">
<value>@((String)context.Variables["MyVariableName"])</value>
</set-header>
</outbound>

Many thanks,
JD

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,431 questions
0 comments No comments
{count} votes

Accepted answer
  1. JananiRamesh-MSFT 29,261 Reputation points
    2022-12-10T06:42:33.057+00:00

    Hi @juni dev Thanks for reaching out. Please refer the sample policy below

    <policies>  
        <inbound>  
            <set-variable name="FirstName" value="@(context.User.FirstName)" />  
        </inbound>  
        <backend>  
        </backend>  
        <outbound>  
            <return-response>  
                <set-status code="200" reason="OK" />  
                <set-body>@((string)context.Variables.GetValueOrDefault("FirstName"))</set-body>  
            </return-response>  
        </outbound>  
        <on-error>  
        </on-error>  
    </policies>  
    

    269181-image.png

    But this will not work if the backend returns 204 response code because as per the standards 204 (No Content) MUST NOT include a message-body.
    reference: https://www.rfc-editor.org/rfc/rfc2616#section-10.1

    Do let me know if you have any queries.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.