9

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.

Justin B.
  • 93
  • 1
  • 1
  • 3
  • using webrequest it should work ? I tried but is not working – Justin B. Nov 23 '17 at 22:59
  • Don't put a standard example into your answer. Instead, show what you tried yourself to solve this problem. – Mikhail Shilkov Nov 23 '17 at 23:12
  • I have tried WebRequest webRequest = WebRequest.Create("https://testing.azurewebsites.net/api/HttpTriggerCSharp1?name=testing"); WebResponse webResp = webRequest.GetResponse(); – Justin B. Nov 23 '17 at 23:18

3 Answers3

12

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);
}
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • error CS1061: 'HttpResponseMessage' does not contain a definition for 'content' and no extension method 'content' accepting a first argument of type 'HttpResponseMessage' could be found (are you missing a using directive or an assembly reference?) – Justin B. Nov 23 '17 at 23:23
  • using System.Net; using System.Net.Http; using System.Web.Http.Controllers; static HttpClient client = new HttpClient(); public static async Task Run(HttpRequestMessage Request) { var url = "https://"; var response = await client.GetAsync(url); string result = await response.Content.ReadAsStringAsync(); return Request.CreateResponse(HttpStatusCode.OK, "Function 1 " + result); } – Justin B. Nov 23 '17 at 23:46
  • I tried the above in azure functions still an error. Can you please help on that? – Justin B. Nov 23 '17 at 23:47
  • Is your binding name `Request`? It's `req' by default. Otherwise code is correct. Which error you get? – Mikhail Shilkov Nov 24 '17 at 06:41
0

I think you should use Durable Functions instead:

public static async Task<object> Run(DurableOrchestrationContext ctx)
{
    var x = await ctx.CallActivityAsync<object>("YourOtherFunctionName");
    // Rest of Function code
}
Kapé
  • 4,411
  • 3
  • 37
  • 54
0

This works for me

var url ="azure function URL with code and params";
using var client = new HttpClient();
client.GetAsync(url).Wait();
Muflix
  • 6,192
  • 17
  • 77
  • 153