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

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

本文介绍了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的形式起作用。但是您可以对POCO对象使用C#HttpTrigger:

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

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>

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

08-16 07:23