简体   繁体   中英

Set body in Azure APIM Policy

My incoming payload is

{ "data":"testdata"}

along with a header X-Type = headerType, I want to map the header into the payload before sending it to the backend system

I want to convert the payload as below

{"data":"testdata","type":"headerType"}

I tried set body but no luck, can some one help

<set-body>@{ 
    var requestBody = context.Request.Body.As<JObject>(preserveContent: 
    true);

    requestBody ["type"] = context.Request.Headers.GetValueOrDefault("X-Type","");

    return requestBody.ToString();
}</set-body>

I'm not able to reproduce your issue.

Maybe it's a typo in question or there's really a space in this line: requestBody ["type"] =

Please do not forget to send the header.

For testing purposes, the set-body policy is placed inside a return-response policy:

Inbound policy:

<inbound>
    <base />
    <return-response>
        <set-status code="200" reason="ok" />
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
        </set-header>
        <set-body>@{
            var requestBody  = context.Request.Body.As<JObject>(true);
            requestBody["type"] = context.Request.Headers.GetValueOrDefault("X-Type","");
            return requestBody.ToString();               
        }</set-body>
    </return-response>
</inbound>

Test in API Management with

  • header X-Type : lorem
  • request body: { "data":"testdata"}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM