Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a query parameter in a custom HTTP route of an Azure Function?

I have an Azure Function and I want to set a custom HTTP endpoint. Following the answer to this SO question, I ended up with something like this:

[FunctionName("DoSomething")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}")]
                HttpRequest request, ILogger logger, string tenantId, string locationId, string manufacturer)
{
        // 
}

However, the route is not accepted by the Webjob:

"v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}"

The reason is because of the question mark '?':

An error occurred while creating the route with name 'DoSomething' and template 'api/v1/tenants/{tenantId}/locations/{locationId}/products?manufacturer={manufacturer}'. The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character. Parameter name: routeTemplate The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character.

Question

How can I specify a query parameter in a custom HTTP endpoint of my Azure Function?

like image 929
Kzrystof Avatar asked Feb 11 '19 18:02

Kzrystof


People also ask

How do you specify a parameter query?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.

How do you pass parameters on an Azure function app?

To pass value in the function route in Azure function, we would have to modify the route parameter as “Hello/{valueName}”. Then add a parameter with the same name as the valueName in the Run method to use this value in your azure function. But adding {valueName} makes it a mandatory value to be passed.

Can you trigger an Azure function using an HTTP request?

Azure Functions may be invoked via HTTP requests to build serverless APIs and respond to webhooks.

What is the difference between a query parameter and a route parameter?

The key difference between query parameters and route parameters is that route parameters are essential to determining route, whereas query parameters are optional.


1 Answers

I am afraid it's not possible to put query parameter in Route.

Microsoft.AspNetCore.Routing: The literal section 'products?manufacturer=' is invalid. Literal sections cannot contain the '?' character.

It's a built-in restriction of ASP.NET Routing, which is used by Azure Function to build route for Http trigger.

allow me to get the value as one of the Run's method parameters instead of poking at the HttpRequest instance

If it's the reason why you want to put query parameter in route, I would suggest you add IDictionary<string, string> query in method signature and use query["manufacturer"] to access the parameter in function code. But honestly it's almost the same as request.Query["manufacturer"].

Or you may have to follow the recommendation, transform the query parameter to route like products/{productId}.

like image 155
Jerry Liu Avatar answered Oct 02 '22 01:10

Jerry Liu