Search code examples
c#azureazure-functionsazure-http-trigger

Azure Functions not accepting an url-encoded value as a query parameter?


I am trying to use a domain as a query param in an Azure Function app.

Something like this:

/api/foo/bar/{url}
/api/foo/bar/https%3a%2f%2flocalhost%3a3000

This doesn't hit the function (although it works locally, although is automatically decodes the : which is annoying), is this as expected? (it does work if I had it as a query string)


Solution

  • You need to use the double encoded URL in the request while running in function app. Pass https%253a%252f%252flocalhost%253a3000 instead of https%3a%2f%2flocalhost%3a3000 while invoking the function in function app.

    I have below function code which is deployed to function app.

    [FunctionName("Function1")]
    public static async Task<IActionResult> 
        Run([HttpTrigger(AuthorizationLevel.Anonymous, 
                         "get", "post", 
                          Route = "foo/bar/{encodedUrl}")] 
                               HttpRequest req,
            string encodedUrl, ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
    
        string decodedUrl = HttpUtility.UrlDecode(encodedUrl);
    
        string name = req.Query["name"];
    
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;
    
        string responseMessage = string.IsNullOrEmpty(name)
            ? $"This HTTP triggered function executed successfully. The decoded URL is: {decodedUrl}. Pass a name in the query string or in the request body for a personalized response."
            : $"Hello, {name}. This HTTP triggered function executed successfully. The decoded URL is: {decodedUrl}.";
    
        return new OkObjectResult(responseMessage);
    }
    

    I am passing the value of encodedUrl as https%253a%252f%252flocalhost%253a3000 and it worked as expected.

    enter image description here