'Send Message to API in APIM from Azure Service Bus using Function App

Is there a way to send a payload that is received on the service buss queue to be sent to an API in APIM using function app? I'm planning to do this on the ServiceBusTrigger function. I already have the message/payload on the QueueMessage, I just need to directly call the API and send the value of the QueueMessage to the API

public static void Run([ServiceBusTrigger("sbq-messagequery", Connection = "ServiceBusConnection")]
    QueueMessage queueItem, ILogger log)
    {
        dynamic data = JsonConvert.SerializeObject(queueItem);
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {data}");
        log.LogWarning($"Id = {queueItem.Payload}");
    }


Solution 1:[1]

You have to do an HttpRequest in your Trigger method.

Please find advice for using the HttpClient in Azure Functions in the documentation

        [FunctionName("MyHttpTrigger")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        var response = await _client.GetAsync("https://microsoft.com");
        var message = _service.GetMessage();

        return new OkObjectResult("Response from function with injected dependencies.");
    }

In your case it might be a POST request:

public static void Run([ServiceBusTrigger("sbq-messagequery", Connection = "ServiceBusConnection")] QueueMessage queueItem, ILogger log)
{
    dynamic data = JsonConvert.SerializeObject(queueItem);
    log.LogInformation($"C# ServiceBus queue trigger function processed message: {data}");
    log.LogWarning($"Id = {queueItem.Payload}");

    var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://yourtarget/api/lorem");
    var json = JsonConvert.SerializeObject(queueItem.Payload);
    requestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
    var response = _client.SendAsync(httpRequestMessage).Result;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1