Azure Http Triggers function calls

Justin B. picture Justin B. · Nov 23, 2017 · Viewed 7.7k times · Source

I would like a Http Trigger function to call another Http Trigger function. Basically, I am trying to access via the URL (HTTP request) the Trigger 1, which that Trigger 1 will call Trigger 2. What I am thinking is to put the fix URL for Trigger 2, so you just call Trigger 1. Any ideas how to do that?

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    name = name ?? data?.name;

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

Any help is much appreciated.

Answer

Mikhail Shilkov picture Mikhail Shilkov · Nov 24, 2017

You can use HttpClient to do a normal HTTP request. Here is what the calling function could look like:

static HttpClient client = new HttpClient();
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req)
{
    var url = "https://<functionapp>.azurewebsites.net/api/Function2?code=<code>";
    var response = await client.GetAsync(url);
    string result = await response.Content.ReadAsStringAsync();
    return req.CreateResponse(HttpStatusCode.OK, "Function 1 " + result);
}