how to encode set-url with parameters

Greg Thomas 121 Reputation points
2023-02-06T00:24:50.3333333+00:00

Hi,

I'm trying to use the send-request policy to send a request to a service URL and then parse the results. The problem I am running into is that my URL has spaces in it, which then get encoded, but when executed to not execute successfully. When I remove the "filter" values, by query runs fine and I see results coming back. But as results go I don't want to be parsing these in apim?

I did try encapsulating my URL in System.Net.WebUtility.UrlEncode - but this did not work.

Thanks

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

2 answers

Sort by: Most helpful
  1. Adrian Gallo 80 Reputation points
    2023-02-06T01:14:55.1633333+00:00

    The System.Web.HttpUtility.UrlEncode method can be used to encode the URL in set-url. Attempt the following code:

    <send-request mode="new" response-variable-name="bearerToken" timeout="20" ignore-error="true">
            <set-url>@{
                string baseUrl = "https://url/api/data/v9.2/data?$select=d1,d2,d3&$filter=d2 eq 'data'";
                string encodedUrl = System.Web.HttpUtility.UrlEncode(baseUrl);
                return encodedUrl;
            }</set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/x-www-form-urlencoded</value>
            </set-header>
            <set-body>@{ return "url"; }</set-body>
    </send-request>
    

    This should correctly encode the URL and eliminate any issues with spaces or special characters.


  2. MuthuKumaranMurugaachari-MSFT 22,151 Reputation points
    2023-02-06T17:18:08.71+00:00

    Greg Thomas Thanks for posting your question in Microsoft Q&A. Based on my understanding, you are trying to set URL with spaces on it and faced some issues while sending. Unfortunately, the code snippet you shared is no longer available and I assume you tried System.Net.WebUtility.UrlEncode which returned error: Value is not a valid absolute URL like below. Is that correct? If not, please share the code snippet and evaluation details for send-request as well as exception.

    send-request (0.242 ms)
    {
        "messages": [
            {
                "message": "Expression value is invalid.",
                "expression": " return System.Net.WebUtility.UrlEncode(\"https://url/api/data/v9.2/data?$select=d1,d2,d3&$filter=d2 eq 'data'\"); ",
                "value": "https%3A%2F%2Furl%2Fapi%2Fdata%2Fv9.2%2Fdata%3F%24select%3Dd1%2Cd2%2Cd3%26%24filter%3Dd2+eq+%27data%27",
                "details": "Value is not a valid absolute URL."
            },
            "Expression value is invalid. Value is not a valid absolute URL."
        ]
    }
    

    Just to let you know, the supported types are listed here, and you can go with either of the below approaches:

    #1

    <set-url>@("https://url/api/data/v9.2/data?" + System.Net.WebUtility.UrlEncode("$select=d1,d2,d3&$filter=d2 eq 'data'"))</set-url>  
    
    

    #2

    <set-url>@{
    
    string baseUrl = "https://url/api/data/v9.2/data?$select=d1,d2,d3&$filter=d2 eq 'data'";
    
    return new System.Uri(baseUrl).AbsoluteUri;
    
    }</set-url>
    
    

    Please let me know if you face any issues with the above approaches. Would be happy to answer any.

    0 comments No comments