set-body policy not working

Suresh Thakur, Kirti 86 Reputation points
2021-11-26T08:02:35.353+00:00

My APIM is receiving request payload in below format -

--newreq
GET Abc?$format=json HTTP/1.1
--newreq
GET Abc?$format=json HTTP/1.1
--endreq--

and I have to format and send to backend in following format --

--newreq
Content-Type: application/http
Accept: application/json

GET Abc?$format=json HTTP/1.1


--newreq
Content-Type: application/http
Accept: application/json

GET Abc?$format=json HTTP/1.1

--newreq--

I have to add the 2 parameters after every --newreq line and append 2 new lines after GET request line.

I have added below policy but it is only appending data after only first --newreq.

Below is my policy

<set-body>@{
                string inBody = context.Request.Body.As<string>(preserveContent: true);
                string[] textSplit = inBody.Split();
                string newbody = string.Empty;
                string strpostBody= "Content-Type:"+"application/http"+" "+"Accept:"+"application/json";
                foreach (string val in textSplit)
                {
                    if (val == "--newreq")
                    {
                        int indexofbatch = inBody.IndexOf("--newreq");
                        if(indexofbatch >= 0)
                        {
                            newbody = inBody.Insert(indexofbatch + "--newreq".Length," "+strpostBody+" ");
                        }
                    }
                }
                return newbody.ToString();
        }</set-body>
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,454 questions
0 comments No comments
{count} votes

Accepted answer
  1. MayankBargali-MSFT 70,941 Reputation points Moderator
    2021-11-30T10:15:27.377+00:00

    @Suresh Thakur, Kirti APIM does support C# 7 in the policy expression. You can refer to API Management Policy Expressions documents for more details on .NET Framework types allowed in policy expressions.

    Input Body:

    --newreq  
    GET Abc?$format=json HTTP/1.1  
    --newreq  
    GET Abc?$format=json HTTP/1.1  
    --endreq--  
    

    Policy Expression:

    <set-body>@{  
                        string inBody = context.Request.Body.As<string>(preserveContent: true);  
                        string[] textSplit = Regex.Split(inBody, "--newreq");  
                        string newString = String.Empty;  
                        string strpostBody = "Content-Type:" + "application/http" + "\n" + "Accept:" + "application/json";  
          
                        for(int i=0; i < textSplit.Length-1;i++)  
                        {  
                            newString += "--newreq \n" + strpostBody + textSplit[i+1];  
                        }  
                        return newString;  
                }</set-body>  
    

    The below body is set:
    --newreq
    Content-Type:application/http
    Accept:application/json
    GET Abc?$format=json HTTP/1.1
    --newreq
    Content-Type:application/http
    Accept:application/json
    GET Abc?$format=json HTTP/1.1
    --endreq--

    You can modify the c# expression as per your need and test different use cases. Feel free to get back to me if you need any assistance.

    0 comments No comments

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.