Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Http Triggers function calls

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.

like image 288
Justin B. Avatar asked Nov 23 '17 22:11

Justin B.


People also ask

How are Azure Functions triggered?

Trigger typesExecute a function when a file is uploaded or updated in Azure Blob storage. Execute a function when a message is added to an Azure Storage queue. Execute a function when a document changes in a collection. Execute a function when an event hub receives a new event.

How run HTTP trigger Azure function in Visual Studio?

To start your function in Visual Studio in debug mode: Press F5. If prompted, accept the request from Visual Studio to download and install Azure Functions Core (CLI) tools. You might also need to enable a firewall exception so that the tools can handle HTTP requests.


1 Answers

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);
}
like image 99
Mikhail Shilkov Avatar answered Oct 16 '22 23:10

Mikhail Shilkov