question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoking a non-HTTP triggered function locally using the Azure Functions Core Tools Admin API (cardinality: one)

See original GitHub issue

Is your question related to a specific version? If so, please specify:

Found Python version 3.8.5 (python3). Azure Functions Core Tools (3.0.2931 Commit hash: d552c6741a37422684f0efab41d541ebad2b2bd2) Function Runtime Version: 3.0.14492.0

What binding does your question apply to, if any? (e.g. Blob Trigger, Event Hub Binding, etc)

Event Hub Trigger Binding (cardinality: one)

Question

Hi there,

we are trying to follow the documentation about how to pass test data to a non-HTTP triggered function to invoke a Python function locally when running within the Azure Functions Core Tools host.

For all kinds of functions other than HTTP triggers and webhooks and Event Grid triggers, you can test your functions locally by calling an administration endpoint. Calling this endpoint with an HTTP POST request on the local server triggers the function.

We are using the EventHub Trigger binding, so the function signature looks like that:

def main(events: List[func.EventHubEvent]):
    # Process all events.
    for event in events:
        pass

Now, we are trying to figure out how exactly to formulate what to put into <trigger_input> in our scenario.

The message body is required to have the following JSON format:

{
    "input": "<trigger_input>"
}

The <trigger_input> value contains data in a format expected by the function.

When submitting an obviously wrong request like

http http://0.0.0.0:7071/admin/functions/eventHubTrigger input=foobar

the host says

[2020-10-14T15:03:03.439] Executed 'Functions.EventHubTrigger' (Failed, Id=57244492-47f9-4801-a99f-431f64a2cd6e, Duration=86ms)
[2020-10-14T15:03:03.439] System.Private.CoreLib: Exception while executing function: Functions.EventHubTrigger. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'events'. Microsoft.Azure.WebJobs.Host: Binding parameters to complex objects (such as 'Object') uses Json.NET serialization.
[2020-10-14T15:03:03.439] 1. Bind the parameter type as 'string' instead of 'Object' to get the raw values and avoid JSON deserialization, or
[2020-10-14T15:03:03.439] 2. Change the queue payload to be valid json. The JSON parser failed: Error parsing boolean value. Path '', line 1, position 1.

When naively sending real JSON like {"input": [{"device-id": "1"}]}, the host responds with <Response [400]> (Bad Request).

After these observations, we started digging through the source code of the host and the python function worker in order to figure out how to build an appropriate payload reflecting the “real” thing but quickly got lost.

While it is obvious that there will be more efforts required to send a complex object instead of a simple scalar string as outlined within the example

curl --request POST -H "Content-Type:application/json" --data '{"input":"sample queue data"}' http://localhost:7071/admin/functions/QueueTrigger

we are now wondering if that would be possible at all. Maybe you can help us out?

Thank you very much in advance and with kind regards, Andreas.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
kepalascommented, Oct 29, 2020

@amotl If I understood your problem correctly, I think that I found the “magic” format that needs to be submitted through the POST call.

This is how I was able to use the Admin API with Event Hub trigger function:

curl --request POST 'http://localhost:7071/admin/functions/MyFunctionName' --header 'Content-Type: application/json' --data-raw '{"input": "{\"SystemProperties\":{},\"myteststuff\":{\"testid\":\"810d9efd-458c-49f2-a9c0-822c897a02e3\",\"testtimestamp\":\"2020-10-28 23:24:16.315294\"},\"testdata\":\"Ping from Az CLI IoT Extension #1\"}"}'

First thing, the JSON in the POST request needs to follow this pattern: {"input":"<escaped JSON string>"}. In addition, the escaped JSON string must include "SystemProperties":{} object.

If the "SystemProperties":{} object is missing, the Admin API still returns a 202 response, but in the host logs you can find the following error:

System.Private.CoreLib: Exception while executing function: Functions.MyFunctionName. System.Private.CoreLib: Result: Failure
Exception: TypeError: unable to decode incoming TypedData: unsupported combination of TypedData field 'string' and expected binding type <class 'azure.functions.eventhub.EventHubTriggerConverter'>
1reaction
amotlcommented, Nov 2, 2020

Dear @stefanushinardi, @anirudhgarg, @anthonychu and @Hazhzeng,

may I humbly suggest to add the outcome of this research by @kepalas to the aforementioned documentation at Passing test data to a function > Non-HTTP triggered functions?

The gist is:

  • When supplying a complex real-world JSON object as <trigger_input> value, it must be properly escaped.
  • The escaped JSON string must include a "SystemProperties":{} object.

With kind regards, Andreas.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manually run a non HTTP-triggered function - Azure
Use an HTTP request to run a non-HTTP triggered Azure Functions. ... Get the function's master key; Call the function; Next steps.
Read more >
TIL #4 - Debug non-HTTP Azure Functions Triggers Locally
I am using the Azure Functions Core Tools v4 and a .NET 5.0 isolated Azure Function. Since the Functions are running in a...
Read more >
An Event Hub Triggered Azure Function using Python example
I used Azure Function Core Tools to run and deploy the Azure Function. ... Or, if you want to run your function with...
Read more >
Invoking non-HTTP Azure Functions over HTTP to make ...
This week, I was presenting at IglooConf (Indexing and searching NuGet org with Azure Functions and Search). During one of the demos, ...
Read more >
April 2019 – baeke.info
When you use Azure Functions in a regular App Service Plan or Premium ... The example Azure Function uses an HTTP trigger and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found