我创建了一个 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; }
}
当我像这样发布功能时(我正在使用邮递员):
我收到一条错误消息:([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 的 raw,每次都出现同样的错误。我也试过切换回HttpRequestMessage
并使用req.Content.ReadAsAsync<MyDocument>
,我得到了类似的错误。我是否错误地构建了我的 POST,或者我是否错误地编写了我的 Azure 函数。无论哪种情况,正确的方法是什么?
确保指定标题:
Content-Type: application/json
那么以下主体应该适用于您的代码:
{
"Name": "myUserName",
"Balance": 123.0,
"ShoeSize": 30
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句