How do i fetch a url from a json request body and set it as the backend url?

Sanjana Pushkar 201 Reputation points Microsoft Employee
2022-11-22T11:19:16.777+00:00

I have a middleware Azure API where the consumers POST a json body value such as this

{
"subscriptionId": "42345",
"type": "MailPieceId",
"status": [
{
"failedValues": [
"abc",
"123@"
],
"reason": "Invalid barcodes"
}
],
"customerWebhook": "https://url",
"referenceNumber": "12345"
}

I need to fetch the value of "customerWebhook" from the json request body and set it as the service backend url, as the url in the json body will be different for every new request.

Currently i am using this in my inbound policy.

<set-variable name="myVar" value="@(context.Request.Body.As<JObject>()["customerWebhook"])" />
<set-backend-service base-url="@(((string)context.Variables["myVar"]).ToString())" />

But this is the error that i keep getting.

"message": "Expression evaluation failed.",
"expression": "((string)context.Variables[\"myVar\"]).ToString()",
"details": "Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'System.String'."

Can anyone please let me know a solution in this case.

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

Accepted answer
  1. JananiRamesh-MSFT 29,261 Reputation points
    2022-11-22T15:05:43.83+00:00

    Hi @Anonymous Thanks for reaching out. Please try using the below policy

    263154-image.png

    263132-image.png

    let me know incase of further queries, I would be happy to assist you.

    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 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.