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>
我沒有使用這種方法,因爲我沒有使用C#。我最終將身體傳遞給JS HttpTrigger作爲普通的xml,然後使用節點包[xml2js](https://www.npmjs.com/package/xml2js)將xml轉換爲JS對象文本。 – LeadingMoominExpert