Azure函数将body请求为xml而不是json

时间:2017-10-16 10:53:42

标签: azure postman azure-functions

在Azure功能中创建javascript函数并使用邮递员发送请求正文时,我跟随this example。在Azure函数中,可以使用json格式的请求体来测试函数。是否可以将身体作为xml而不是json发送?使用的请求正文是

{
    "name" : "Wes testing with Postman",
    "address" : "Seattle, WA 98101"
}

1 个答案:

答案 0 :(得分:0)

JS HttpTrigger不支持请求体xml反序列化。它起到了普通xml的作用。但您可以将C#HttpTrigger与POCO对象一起使用:

function.json:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "name": "data",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "name": "res",
      "direction": "out"
    }
  ]
}

run.csx

#r "System.Runtime.Serialization"

using System.Net;
using System.Runtime.Serialization;

// DataContract attributes exist to demonstrate that
// XML payloads are also supported
[DataContract(Name = "RequestData", Namespace = "http://functions")]
public class RequestData
{
    [DataMember]
    public string Id { get; set; }
    [DataMember]
    public string Value { get; set; }
}

public static HttpResponseMessage Run(RequestData data, HttpRequestMessage req, ExecutionContext context, TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request. {req.RequestUri}");
    log.Info($"InvocationId: {context.InvocationId}");
    log.Info($"InvocationId: {data.Id}");
    log.Info($"InvocationId: {data.Value}");

    return new HttpResponseMessage(HttpStatusCode.OK);
}

请求标题:

Content-Type: text/xml

请求正文:

<RequestData xmlns="http://functions">
    <Id>name test</Id>
    <Value>value test</Value>
</RequestData>