2017-10-16 21 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

相关问题