Amitay Strijevski
Amitay Strijevski

Reputation: 96

How to pass array as url parameter to Azure Function

Having a bit of problem with Azure Functions parameters I understood that url parameters are sent as usual to Azure Function "www.asdf.com?myParam=arnold" and being read like this

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

What i don't understant is how to send array as a parameter.

Upvotes: 2

Views: 5014

Answers (2)

Mikhail Shilkov
Mikhail Shilkov

Reputation: 35134

One way would be to send parameters like this:

www.asdf.com?myParam=arnold&myParam=james&myParam=william

and then read them as

var params = req
    .GetQueryNameValuePairs()
    .Where(q => string.Compare(q.Key, "myParam", true) == 0)
    .Select(q => q.Value);

Upvotes: 4

ing.alfano
ing.alfano

Reputation: 436

for complex data I would suggest you to pass it in the body of a POST request as a json, then you can deserialize it in a dynamic object or a Jobject or a custom class you can define. Here is an example from the Azure docs

#r "Newtonsoft.Json"

using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{    
    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);

    log.Info($"WebHook was triggered! Comment: {data.comment.body}");

    return req.CreateResponse(HttpStatusCode.OK, new {
        body = $"New GitHub comment: {data.comment.body}"
    });
}

In this example the request body is deserialized in data object. The request body contains a comment property and comment contains a body property as follows

{
    "comment": {
        "body": "blablabla"
    }
}

Of course in the json you can add as many arrays as you need

Hope it helps

Upvotes: 4

Related Questions