我创建了一个 Azure 函数并在本地运行它:
[FunctionName("HttpTriggerCSharpSet")]
public static async Task<HttpResponseMessage> Set([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] MyDocument req, TraceWriter log)
{
// ...
}
请注意MyDocument
是第一个参数而不是HttpRequestMessage
。我在文档中读到这种方法应该可行,并且它看起来与 ASP.NET 模型绑定非常相似(无论如何,在我看来)。MyDocument
是一个只有 3 个属性的 POCO。
public class MyDocument
{
public string Name { get; set; }
public int ShoeSize { get; set; }
public decimal Balance { get; set; }
}
当我像这样发布到函数时(我正在使用 Postman):
![POST with Postman to Azure Function](https://i.stack.imgur.com/Nw2Lv.png)
我收到一条错误消息:[8/8/2017 2:21:07 PM] Exception while executing function: Functions.HttpTriggerCSharpSet. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'req'. System.Net.Http.Formatting: No MediaTypeFormatter is available to read an object of type 'MyDocument' from content
(您也可以在上面的 Postman 截图中看到)
我尝试过 form-data 和 x-www-form-urlencoded 甚至来自 Postman 的原始数据,每次都出现同样的错误。我也尝试过切换回HttpRequestMessage
并使用req.Content.ReadAsAsync<MyDocument>
,我收到类似的错误。我是否错误地构建了我的 POST,或者我是否错误地编写了我的 Azure Functions。无论哪种情况,正确的方法是什么?
确保指定标头:
Content-Type: application/json
那么以下主体应该适用于您的代码:
{
"Name": "myUserName",
"Balance": 123.0,
"ShoeSize": 30
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)