4

Is this currently possible per function? By "methods" i mean multiple HTTP verbs, like "get", "post", "put", etc.

In Web API using controllers we could do that by assigning attributes that distinct method calls in a controller class.

Is there anything like this in azure functions?

Kasbolat Kumakhov
  • 607
  • 1
  • 11
  • 30

1 Answers1

6

Yes it is possible to specify one or more http methods for a function via the methods property in the function.json file for your function. By default methods is not specified meaning the function accepts all methods. When you specify a restricted set, only those methods are allowed, and any other methods will result in a 405 "Method Not Allowed" response.

{
    "bindings": [
        {
            "type": "httpTrigger",
            "name": "req",
            "direction": "in",
            "methods": [ "post", "put" ]
        },
        {
            "type": "http",
            "name": "$return",
            "direction": "out"
        }
    ]
}

We'll be releasing some big improvements in this area vary soon. We'll be supporting custom http routes, with full route templating etc. which will allow you to define REST APIs in the way you'd expect. Using this new functionality, you can have one function handling GET requests for a resource, and another handling PUT/POST, both using a restful route scheme like products/{category}/{id?}. These upcoming changes will allow you to accomplish all the WebAPI routing scenarios.

mathewc
  • 13,312
  • 2
  • 45
  • 53
  • So i suppose to distinct these methods in the function itself i have to use something like "if (req.Method == HttpMethod.Post) PostMethod(req, log) else GetMethod(req, log)" ? – Kasbolat Kumakhov Oct 14 '16 at 05:56
  • Yes, if multiple methods are mapping to a single function, you'll have to have the switch logic in your code. Again this will be made easier with the upcoming http routing improvements about to be released :) – mathewc Oct 14 '16 at 14:35
  • Thanks! These additions indeed will be very useful! – Kasbolat Kumakhov Oct 16 '16 at 07:56
  • 2
    Just curious if these improvements ever were released. Are they the function proxies? – Graham Jul 25 '17 at 16:52
  • BTW, this answer and the comments helped me get it running - thanks! – Graham Jul 25 '17 at 19:24