2017-10-16 22 views
2

我在使用郵遞員在Azure函數中創建JavaScript函數併發送請求正文時,正在關注this example。在Azure函數中,可以使用json格式的請求主體來測試函數。是否有可能發送身體爲XML而不是JSON?使用的請求正文是Azure函數請求正文爲xml而不是json

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

回答

0

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> 
+0

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

相關問題