【问题标题】:Azure functions request body as xml instead of jsonAzure 函数请求正文为 xml 而不是 json
【发布时间】:2018-03-27 20:58:49
【问题描述】:

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

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

【问题讨论】:

    标签: azure postman azure-functions


    【解决方案1】:

    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"
        }
      ]
    }
    

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

    【讨论】:

    • 我没有使用这种方法,因为我没有使用 C#。我最终将正文作为纯 xml 传递给 JS HttpTrigger,然后使用节点包 xml2js 将 xml 转换为 JS 对象文字
    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 2012-02-04
    • 2018-07-28
    • 2019-06-02
    • 2021-12-17
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多