Azure 函数请求正文为 xml 而不是 json

人气:1,034 发布:2022-09-11 标签: azure azure-functions postman

问题描述

我正在关注 这个例子 在 Azure 函数中创建 javascript 函数并使用邮递员发送请求正文时.在 Azure 函数中,可以使用 json 格式的请求正文来测试函数.是否可以将正文作为 xml 而不是 json 发送?使用的请求正文是

I'm following this example when creating a javascript function in Azure functions and sending the request body using postman. In Azure functions it's possible to test the function using the request body which is in json format. Is it possible to send the body as xml instead of json? The request body used is

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

推荐答案

JS HttpTrigger 不支持请求体 xml 反序列化.它以普通 xml 的形式发挥作用.但是您可以将 C# HttpTrigger 与 POCO 对象一起使用:

JS HttpTrigger does not support request body xml deserialization. It comes to function as plain xml. But you can use C# HttpTrigger with POCO object:

function.json:

function.json:

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

运行.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>

276