none
How to pass object to http trigger Azure Functions process it and return Some value RRS feed

  • Question

  • using System.Net;
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,CustomerDetail customers,TraceWriter log)
    {
        log.Info("C# HTTP trigger function received an order.");
        log.Info(req.ToString());
        log.Info("Submitting to processing queue.");

     if (customers.ID == "112sadsa12")

    {

     calculate something

    }

      if (req.orderId == null)
        {
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        }
        else
        {
            await outputQueueItem.AddAsync(req);
            return new HttpResponseMessage(HttpStatusCode.OK);
        }
    }

    class CustomerDetail 

    {

    public string Name;

    public string Email;

    public string ID;

    }

    Tuesday, December 6, 2016 11:44 AM

Answers

  • Also check this URL that shows how to use Newtonsoft.Json to parse the payload into an object,

    http://stackoverflow.com/questions/38094170/how-can-i-parse-json-in-a-azure-function

    ---

    #r "Newtonsoft.Json"
    
    using System.Net;
    using Newtonsoft.Json;
    
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    {
        dynamic body = await req.Content.ReadAsStringAsync();
        var e = JsonConvert.DeserializeObject<EventData>(body as string);
        ... use e here ...
    }
    
    public class EventData
    {
        public string Category { get; set; }
        public string Action { get; set; }
        public string Label { get; set; }
    }


    Thursday, December 15, 2016 8:30 AM

All replies

  • This question is not clear at all. Please clarify what you're trying to do and what problem you're having.

    Mathew Charles [MSFT]

    Tuesday, December 6, 2016 4:36 PM
  • Typically you would be passing either query string arguments, or a payload into the http request. Not a strongly typed object instance,

    ---

    GET - http://yourfunctionurl.com/functionname?arg1=foo&arg2=bar

        string foo = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "foo", true) == 0)
            .Value;

    ---

    POST - http://yourfunctionurl.com/functionname

    {
      "arg1": "foo",
      "arg2": "bar"
    }

    string data = req.Content.ReadAsString();
    JObject jsonDATA = JObject.Parse(data);
    string foo = jsonDATA["foo"].Value<String>();

    ---

    The code above is copy, pasted, hacked, untested.  But should give you an idea.

    Nick.


    Wednesday, December 14, 2016 11:53 AM
  • Also check this URL that shows how to use Newtonsoft.Json to parse the payload into an object,

    http://stackoverflow.com/questions/38094170/how-can-i-parse-json-in-a-azure-function

    ---

    #r "Newtonsoft.Json"
    
    using System.Net;
    using Newtonsoft.Json;
    
    public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
    {
        dynamic body = await req.Content.ReadAsStringAsync();
        var e = JsonConvert.DeserializeObject<EventData>(body as string);
        ... use e here ...
    }
    
    public class EventData
    {
        public string Category { get; set; }
        public string Action { get; set; }
        public string Label { get; set; }
    }


    Thursday, December 15, 2016 8:30 AM