首页 > 解决方案 > 无法从另一个天蓝色函数运行 HttpTrigger Azure 函数

问题描述

这是 function.json 片段

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "query",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]

这是我的 run.csx 文件:

public class Query{
    public double AdvanceDays{get;set;}
}
public static async Task<HttpResponseMessage> Run(Query query,entity Entity,HttpRequestMessage req, TraceWriter log, IEnumerable<dynamic> inputDocument)
{

现在我需要从另一个函数调用这个 Http 触发函数。我在做:

var url = "https://azurefunction...";
var content2 = new StringContent(string.Format("{{\"AdvanceDays\":7}}"));
var response = await client.PostAsync(url,content2);

但我收到以下错误: No MediaTypeFormatter is available to read an object of type 'Query' from content with media type 'text/plain'.

我该如何解决?请帮忙!!!

标签: c#azurehttp-postazure-functions

解决方案


看起来问题是您发送的请求未指定其内容类型,并且默认为text/plain. 由于没有Content-type header,因此该函数不知道您要发送哪种类型的内容。您应该添加Content-Type header带有值的 a application/json

Accept request-header 字段可用于指定响应可接受的某些媒体类型。

Content-Type entity-header 字段指示发送给接收者的实体主体的媒体类型,或者在 HEAD 方法的情况下,如果请求是 GET,则本应发送的媒体类型。

资料来源:接受标头与内容类型标头


推荐阅读