Nested Http Route in Azure Function returns 404 not found - node.js

I have multiple functions handling GET, PUT & POST API's with different paths in my Azure Function and they all work fine. Each function has one index.js file handling one HttpMethod only & I have a routePrefix of "api" in the hosts.json. The function.json for one of the functions can be found below
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "bike/{id}/like",
"methods": [
"put"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
The problem I am facing is the two PUT requests below don't work & I get a 404 not found. However this seems to work when I run the function locally in vscode & check using Postman.
http://<MyAzureFuncName>.azurewebsites.net/api/bike/63d51c0bb593aec8734638e5/like
http://<MyAzureFuncName>.azurewebsites.net/api/bike/63d51c0bb593aec8734638e5/dislike
UPDATE 1
I have just found out that calling the above API's using the test/run feature on the Azure function in the portal works but not when I try using Postman. Any ideas why this is the case
I also have GET & an update method for PUT following a similar signature (shown below) which both work locally but on Azure the PUT is returning the GET result when checking with Postman. Could this PUT overload be the cause for the mix up.
http://<MyAzureFuncName>.azurewebsites.net/api/bike/63d51c0bb593aec8734638e5
UPDATE 2
I have found that all the above PUT requests work if I use https instead of http. The other POST, DELETE & GET requests work with both. This is all the more baffling, can anyone shed some light on this.
I would appreciate any help & guidance in spotting the obvious.
UPDATE 3
I do not have the TLS/SSL settings (classic) option in the Settings for my function app or in any of my app services for that matter. I am assuming this was something on the old portal.
Thanks

Since you are able to reach the endpoint with https and not with http, I assume that the error is the HTTP Only setting.
Navigate to your Function App, and go to TLS/SSL settings (classic) in the Settings section and switch HTTPS Only to Off.
When it comes to this section:
I also have GET & an update method for PUT following a similar
signature (shown below) which both work locally but on Azure the PUT
is returning the GET result when checking with Postman. Could this PUT
overload be the cause for the mix up.
I would say it's normal to return the resource when making a PUT request. For example, if you update the resource, you would then return the data with the updated information.
Hope it helps.

Related

presetOverride when creating Azure Media Services v3 Job

When creating an Azure Media Services Job via the REST API, I cannot set a presetOverrides property on the JobOutputAsset as defined in the documentation: https://learn.microsoft.com/en-us/rest/api/media/jobs/create#joboutputasset
My request body is:
{
"properties": {
"input": {
"#odata.type": "#Microsoft.Media.JobInputAsset",
"assetName": "inputAsset"
},
"outputs": [
{
"#odata.type": "#Microsoft.Media.JobOutputAsset",
"assetName": "outputAsset",
"label": "en-US",
"presetOverride": {
"#odata.type": "#Microsoft.Media.AudioAnalyzerPreset",
"audioLanguage": "en-US",
"mode": "Basic"
}
}
],
"priority" : "Normal"
}
}
The error message thrown is:
{
"error": {
"code": "InvalidResource",
"message": "The property 'presetOverride' does not exist on type 'Microsoft.Media.JobOutputAsset'. Make sure to only use property names that are defined by the type."
}
}
When removing the presetOverride data, everything works as expected. The official documentation clearly states that the Microsoft.Media.JobOutputAsset does have a presetOverride property though. What am I doing wrong?
It is important to select the correct API version when communicating with the Azure Media Services REST API.
In this case, api version 2020-05-01 from the Azure Media Services Postman examples was used. But the presetOverride option is only available starting with version 2021-06-01.
Setting api-version=2021-06-01 as a GET parameter enables Preset Overrides.
couple of concerns here Rene. I would not recommend using the raw REST API directly for any Azure services. Reason being is that there are a lot of built-in retry scenarios and retry policies that are already rolled into the client SDKs. We've had many customers try to roll their own REST API library but run into massive issues in production because they failed to read up on how to handle and write their own custom retry policy code.
Unless you are really familiar with rolling your own retry policies and how Azure Resource Management gateway works, try to avoid it and just use the official client SDKs - see here - https://learn.microsoft.com/en-us/azure/architecture/best-practices/retry-service-specific#general-rest-and-retry-guidelines
Now, to answer your specific question - try using my sample here in .NET and see if it answers your question.
https://github.com/Azure-Samples/media-services-v3-dotnet/blob/3ab85647cbadd2b868aadf175afdede67b40b2fd/AudioAnalytics/AudioAnalyzer/Program.cs#L129
I can also provide a working sample of this in Node.js/Typescript in this repo if you like. It is using the latest 10.0.0 release of our Javascript SDK.
I'm working on samples in this repo today - https://github.com/Azure-Samples/media-services-v3-node-tutorials
UPDATE: Added basic audio in Typescript sample.
https://github.com/Azure-Samples/media-services-v3-node-tutorials/blob/main/AudioAnalytics/index.ts
Shows how to use the preset override per job.

Azure Function GET request getting 401, but POST works

I have an Azure function that has a GET endpoint and a POST endpoint. In local testing they both work fine. When published to Azure, the POST endpoint works with the copied link from the Functions blade ('https://address/api/search?code=xxx'), but a GET request to the same endpoint returns 401 Unauthorized. Anyone else have this issue, or an idea on how to get this working? I've tried restarting the Function app, re-publishing, and stop-starting the app. All result in the same issue.
EDIT: Just for clarity, I'm using POST to do a detailed search with search terms included in the body, and GET to just retrieve all items.
EDIT2: I also see that the function.json visible in the Functions blade shows the following:
..."bindings": [
{
"type": "httpTrigger",
"route": "search",
"methods": [
"post"
],
"authLevel": "function",
"name": "req"
}
], ...
So it looks like my GET endpoint didn't even make it into the published function. This works locally with two functions sharing an endpoint, but differing request types, but doesn't seem to work in Azure. Any idea why?
I just figured it out. The functions in the Functions blade are listed by function name, not endpoint. I had built the functions in Visual Studio, so the compiler required unique names for the functions, which made sure I was naming them properly. I just missed the fact that each function name was separate, regardless of endpoint.

Azure Search, listAdminKeys, ARM output error (does not support http method 'POST')

I am using this bit of code as an output object in my ARM template,
"[listAdminKeys(variables('searchServiceId'), '2015-08-19').PrimaryKey]"
Full text sample of the output section:
"outputs": {
"SearchServiceAdminKey": {
"type": "string",
"value": "[listAdminKeys(variables('searchServiceId'), '2015-08-19').PrimaryKey]"
},
"SearchServiceQueryKey": {
"type": "string",
"value": "[listQueryKeys(variables('searchServiceId'), '2015-08-19')[0]]"
}
I receive the following error during deployment (unfortunately, any error means the template deployment skips output section):
"The requested resource does not support http method 'POST'."
Checking the browser behavior seems to validate the error is related to the function (and, it using POST).
listAdminKeys using POST
How might I avoid this error and retrieve the AzureSearch admin key in the output?
Update: the goal of doing this is to gather all the relevant bits of information to plug into other scripts (.ps1) as parameters, since those resources are provisioned by this template. Would save someone from digging through the portal to copy/paste.
Thank you
You error comes from listQueryKeys, not admin keys.
https://learn.microsoft.com/en-us/rest/api/searchmanagement/adminkeys/get
https://learn.microsoft.com/en-us/rest/api/searchmanagement/querykeys/listbysearchservice
you wont be able to retrive those in the arm template, it can only "emulate" POST calls, not GET
With the latest API version, it's possible to get the query key using this:
"SearchServiceQueryKey": {
"type": "string",
"value": "[listQueryKeys(variables('searchServiceId'), '2020-06-30').value[0].key]"
}

Get route url for a http triggered functions in an ARM template

I'm trying to figure out how to get the route for an HTTP triggered Azure Function within an ARM template.
Thanks to a blog post I managed to find out the listsecret command, but when trying to execute this action via powershell, the output doesn't give me the trigger_url I was expecting. The URL does not comply with the configured route of the function, and shows the default trigger if no route would have been configured.
Any way I can get a hold of the configured route instead since I can't seem to use the trigger_url.
My configured route has got parameters in the path as well, e.g.:
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"authLevel": "function",
"methods": [
"POST"
],
"route": "method/{userId}/{deviceId}"
}
The output of listsecrets is:
trigger_url: https://functionapp.azurewebsites.net/api/method?code=hostkey
Is there any other way to extract the host key and route?
Try playing with the API version, but I would suspect that this is not possible as of now.
Currently, the only way to get the route is by reading the function.json file and parsing that information out, which you can do by using Kudu's VFS API.
For the keys, I would actually recommend using the key management APIs instead of listSecrets. As the latter is meant to address a small set of scenarios (primarily to enable some internal integrations) where the key management API more robust API and will continue to work with different secret storage providers (e.g. Azure Storage, which is what is used when slots are enabled and will eventually become the default).

Get id or name of Azure Webjob run when triggered externally

when I start a webjob using the rest api :
/api/triggeredwebjobs/{job name}/run?arguments={arguments}
I need to know if the program invoked ran successfully or not and for now I request the latest result from the history using .../api/triggeredwebjobs/{job name}/history
Now, is there a way to get the {id} of the Job just after I invoke ? Because obviously there's no way to be sure that the latest history is the job I just ran. Or is there another way to get things done?
Thanks.
Yes, we added a new binder in the extensions library to allow you to get the instance ID - ExecutionContext. See an example here in the extensions repo samples. To use this binding you'll have to pull in the beta1 Microsoft.Azure.WebJobs.Extensions prerelease package, and add config.UseCore() to your startup code (as the sample app shows). This was added based on another ask similar to yours.
You can call this anywhere in your code and it works !
(Not in debug, but when published)
Console.Out.WriteLine("RUN NAME : " + Environment.GetEnvironmentVariable("WEBJOBS_RUN_ID"));
We just added support for this in the WebJobs API. The way it works is that when you send the POST request to trigger the WebJob, you now get back a location attribute, with a URL to the details of the run that was started. e.g.
Location: https://mysite.scm.azurewebsites.net/api/triggeredwebjobs/SomeJob/history/201605192149381933
You can then query this URL to track the run, e.g.
{
"id": "201605192149381933",
"name": "201605192149381933",
"status": "Success",
"start_time": "2016-05-19T21:49:38.1933956Z",
"end_time": "2016-05-19T21:49:39.4826458Z",
"duration": "00:00:01.2892502",
"output_url": "https://mysite.scm.azurewebsites.net/vfs/data/jobs/triggered/SomeJob/201605192149381933/output_log.txt",
"error_url": null,
"url": "https://mysite.scm.azurewebsites.net/api/triggeredwebjobs/SomeJob/history/201605192149381933",
"job_name": "SomeJob",
"trigger": "External - ARMClient/1.1.1.0"
}

Resources