Azure 函数请求正文为 xml 而不是 json [英] Azure functions request body as xml instead of json

查看:27
本文介绍了Azure 函数请求正文为 xml 而不是 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 这个例子 在 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>

这篇关于Azure 函数请求正文为 xml 而不是 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆