user2022284
user2022284

Reputation: 443

How to customize Azure function url

Just got started with azure functions. I am using it as an httptrigger for an IoT device.

I am trying to setup one function that will work for httptrigger requests coming from several IoT devices - So I dont have to setup one function per device. So ideally, in my c# file, I will have something like this:

DeviceClient deviceClient;
string iotHubUri = "myhub";
string deviceName = "something dynamic here that changes with device";
string deviceKey = "something dynamic here that changes with device";

Then , I'd like to get my function url to look something like this:

"https://<functionapp>.azurewebsites.net/api/<function>/{device_id}?code=xxxxx"

where device_id is the IoT device id.

I am not sure how to first setup the reference in the c# file to be dynamic and also how to get the url to look the way I intend.

Some help will be appreciated. Thanks

Upvotes: 8

Views: 8749

Answers (2)

Carlos
Carlos

Reputation: 5052

Customize your Azure function route

To customize your url you can use two files, the one in which is defined the function function.json and the host.json.

function.json suffix

in function.json you can define the base url adding to the bindings array the "route": "yourbaseurl" property. that should result in something like this

{
  "scriptFile": "../dist/server.js",
  "disabled": false,
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "cool", ///// This is the line that you have to add /////
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    }
  ]
}

host.json prefix

On the host.jsonfile you can add a prefix to that route, let's say you want to add a fancy name group or so...

therefore your file should look something like this

{
  "version": "2.0",
  "extensions": {
    "http": {    
        "routePrefix": "prefix" ///// here is that you include your prefix route /////
    }
  },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

I just want one word or none

if you want to have just one word in your route leave the host.json "routePrefix": "" and the function.json "route": "therouteyouwant" or empty like "route": "".

Upvotes: 3

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35124

There is a route parameter is HTTP trigger exactly for this. Your trigger definition should look something like this:

"bindings": [
  {
    "type": "httpTrigger",
    "route": "something/{deviceid}",
    "name": "request",
    // possibly other parameters
  }
],

If you are using precompiled C# functions, you can do the same via attribute property, e.g.

public static IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Function, "GET", Route = "something/{deviceid}")] 
    HttpRequest request,
    string deviceid)
{
    // do something based on device id
}

Upvotes: 7

Related Questions