How do I trigger an Azure Webjob with a http call?

MarkP 1 Reputation point
2024-12-20T12:33:01.2333333+00:00

My needs are really simple but I can't get anything to work.

I wish to trigger an Azure webjob by sending a http request.

(Webjob created from the Quickstart guide https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/quickstarts-sdk/image-analysis-client-library-40?tabs=visual-studio%2Cwindows&pivots=programming-language-csharp)

The webjob works fine both on local and when Run from the Azure platform. Now I wish to run it on demand from a button on my website. (And later I'll introduce parameters)

  • I get 'no route registered' when I use the Webhook URL in the webjob
  • When using the Copilot suggested yourfunctionapp.azurewebsites.net/admin/functions/functionnameI get 404 errors

thanks

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,968 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. brtrach-MSFT 17,741 Reputation points Microsoft Employee Moderator
    2024-12-20T23:31:28.0333333+00:00

    @MarkP These are the steps that I followed to trigger my webjob. Can you try them and let me know the outcome?

    1. Create a WebJob: Ensure your WebJob is set up correctly in your Azure App Service. It sounds like you've already done this.
    2. Get the WebJob URL:
      • The URL format for triggering a WebJob is typically: https://<your-app-service-name>.scm.azurewebsites.net/api/triggeredwebjobs/<your-webjob-name>/run.
        • Replace <your-app-service-name> with the name of your Azure App Service and <your-webjob-name> with the name of your WebJob.
    3. Authentication:
      • You need to authenticate your HTTP request. This can be done using basic authentication with your App Service's publishing credentials.
        • You can find these credentials in the Azure portal under your App Service > Deployment Center > FTP/Credentials.
    4. Send the HTTP Request:
      • Use a tool like Postman or write a script to send an HTTP POST request to the WebJob URL with the necessary authentication headers.

    Here's an example using C# and HttpClient:

    using System;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;
    
    class Program
    {
        static async Task Main(string[] args)
        {
            var webJobUrl = "https://<your-app-service-name>.scm.azurewebsites.net/api/triggeredwebjobs/<your-webjob-name>/run";
            var userName = "<your-username>";
            var password = "<your-password>";
    
            using (var client = new HttpClient())
            {
                var byteArray = Encoding.ASCII.GetBytes($"{userName}:{password}");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
    
                var response = await client.PostAsync(webJobUrl, null);
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("WebJob triggered successfully.");
                }
                else
                {
                    Console.WriteLine($"Failed to trigger WebJob. Status code: {response.StatusCode}");
                }
            }
        }
    }
    
    
    

    If you encounter issues like "no route registered" or 404 errors, double-check the URL format and ensure your WebJob is correctly deployed and accessible. Also, verify that your App Service is running and that the WebJob is in the correct state.

    1 person found this answer helpful.
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.