Anand Hegde
Anand Hegde

Reputation: 11

How to decode HTTP request query string in azure APIM policies?

I have URL in the below format, and I want to decode the query string and re-write the URL.

https://api.test.abc.com/parameters?username=%7Busername%7D

How can I decode username using policies and re-write it in the following format:

API Endpoint: /api/affiliated/users/userInfo/?username=abcd.gmail.com

Many thanks in advance.

Upvotes: 0

Views: 1466

Answers (3)

Anand Hegde
Anand Hegde

Reputation: 11

We cannot directly decode, as there is no option, we have to do delete the first query parameter and rewrite the URI by changing and decoding the query parameter. I used the below code snippet to execute that. (as the question was also to replace the userName in the URL with visibleToUserId along with decoding the value.)

@((Convert.ToBase64String(Encoding.UTF8.GetBytes((string)context.Request.MatchedParameters[\"visibleToUserId\"]))).Replace('=', '~').Replace('+', '-').Replace('/', '_'))

Upvotes: 0

Asthiss
Asthiss

Reputation: 221

You can use the System.Net.WebUtility with in the APIM policies

System.Net.WebUtility.UrlDecode(context.Request.Url.Query["username"]

Upvotes: 1

vinod rawool
vinod rawool

Reputation: 53

You can decode it manually.

context.Request.Url.Query["username"] | Replace:'%7B','{' | Replace:'%7D','}'

Save that username in one variable and use it in rewrite template.

I too want to know some method to decode url in APIM policy.

Upvotes: 0

Related Questions