使用模型作为参数调用 azure 函数时出错

Error calling azure function with model as parameter

我一直收到错误消息:

Exception while executing function: Functions.InsertItem -> Exception binding parameter 'newItem' -> No value was provided for parameter 'newItem' 

当调用下面的 azure 函数时。

我其实是用azure portal开发的,没有VS

public static async Task<HttpResponseMessage> Run( HttpRequestMessage req,
[HttpTrigger(AuthorizationLevel.Function, "POST")] Item newItem)
{                  
     return newItem == null
         ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass an item in the request body")
         : req.CreateResponse(HttpStatusCode.OK, "Hello " + newItem.Name);
} 

public class Item
{  
    public string Name { get; set; }    
}

知道哪里出了问题吗?谢谢!

例如,如果我们想要 post 一个 JSON 对象按如下方式运行

{
  "Item":
   { 
      "Name" : "test details"
   }
}

然后函数必须更新如下,如果要进行显式转换或在代码中使用动态对象反序列化请求,则需要使用 JSON 反序列化器。

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");


    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    string name = data.Item?.Name;

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

public class Item
{
    public string Name{get;set;}
}

下面是来自function.json

的绑定细节
{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

请将您的 function.json 更改为以下内容:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "newItem",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

您不应该为 newItem 设置单独的条目,只需将其声明为触发器即可。