Share via

Azure Function: Missing a trigger argument

Fabian Homann 46 Reputation points
2023-12-07T13:47:02.2466667+00:00

Hello!

I created a Azure Function Trigger.

This is the run.csx:

    [FunctionName("CreateContactInDynamics")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        string firstName = data?.firstName;        string lastName = data?.lastName;        var token = await GetDynamicsAccessToken(log);        await CreateContactAsync(firstName, lastName, token);        return new OkObjectResult($"Contact created: {firstName} {lastName}");    }

function.json:

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

Error Msg.: AF003: Missing a trigger argument named 'req'

Hope you can help me out here :-)

Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

0 comments No comments

Answer accepted by question author

MuthuKumaranMurugaachari-MSFT 22,451 Reputation points Moderator
2023-12-07T16:36:41.81+00:00

Fabian Homann Thanks for posting your question in Microsoft Q&A. Based on your reference to run.csx, I assume you are creating this function via azure portal as C# script. Correct me if that's not the case.

You can simply define bindings like HTTP trigger in function.json and pass the name req in the argument without needing to add HttpTrigger attribute. Check out HTTP trigger doc for detailed info which has example like below:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];
    
    string requestBody = String.Empty;
    using (StreamReader streamReader =  new  StreamReader(req.Body))
    {
        requestBody = await streamReader.ReadToEndAsync();
    }
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;
    
    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

However, if you are using in-process model (or isolated model), then HttpTriggerAttribute is required as described in https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cin-process%2Cnodejs-v4%2Cfunctionsv2&pivots=programming-language-csharp#attributes.

User's image

I hope this helps and let me know if you have any questions.

Update:

Fabian Homann resolved the issue after removing HttpTrigger attribute and fixed static class in the code.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.