I have a condition like: In the outbound I will check the response of the GET API which is where aAPI policies are there. If the body has a key with entityStatus which takes values 0 or 1, if 0 then output simple message else need to send-request to another API endpoint which is POST API.
I used API policies as below:
<set-variable name="id" value="" />
<set-variable name="newRequest" value="@(context.Request.Body?.As<JObject>
(preserveContent: true))" />
<choose>
<when condition="@(context.Response.StatusCode == 200 &&
(int)context.Response.Body?.As<JObject>(true)["entityStatus"] == 1)">
<send-request mode="new" timeout="20" response-variable-name="id" ignore-error="false">
<set-url>@($"https://api.dev/external/workrequest/wr")</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="Authorization" exists-action="override">
<value>@(context.Request.Headers.GetValueOrDefault("Authorization","scheme param"))</value>
</set-header>
<set-body>@{ var document = (JObject)context.Variables["newRequest"];
return document?.ToString();
}</set-body>
</send-request>
<return-response response-variable-name="id">
<set-status code="200" reason="OK" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@((((IResponse)context.Variables["id"]).Body.As<JObject>()).ToString())</set-body>
</return-response>
</when>
<when condition="@(context.Response.StatusCode == 200 && (int)context.Response.Body?.As<JObject>(true)["entityStatus"] == 0)">
<return-response>
<set-status code="500" reason="VOID" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value></set-header>
<set-body>The record is void</set-body>
</return-response>
</when>
<otherwise />
The response it returned was good when entityStatus was 0 but when entityStatus is 1 it was returning a 400 error. The error is like:
{
"type": "https://httpstatuses.io/400",
"title": "Bad Request",
"status": 400,
"traceId": "00-317b53ba7a76f19b6153ca6ab14c5190-e9c27cf9162e2414-00"
}
I just need the response from the POST API which I am sending the request to and how can I do this?
CodePudding user response:
I got a solution, I was using below variable that will take request body:
<set-variable name="newRequest" value="@(context.Request.Body?.As<JObject>(preserveContent: true))" />
I changed it by using:
set-variable name="newRequest" value="@(context.Response.Body.As<JObject>(preserveContent: true))" />