3

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"
}
Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36

1 Answers1

0

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:

{
  "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);
}

Request header:

Content-Type: text/xml

Request body:

<RequestData xmlns="http://functions">
    <Id>name test</Id>
    <Value>value test</Value>
</RequestData>
Alexey Rodionov
  • 1,436
  • 6
  • 8
  • I didn't use this approach since I'm not using C#. I ended up passing the body to the JS HttpTrigger as a plain xml and then converted the xml into JS object literal using the node package [xml2js](https://www.npmjs.com/package/xml2js) – LeadingMoominExpert Oct 19 '17 at 09:57