1

I want to trigger HttpTrigger function every day at 9.00 PM using the Timer trigger. Is it possible in Azure function for python?

I am not able to understand how can I implement it? Is there any way to implement the above scenario?

Please check the below code.

HttpTrigger Function Code

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')
            logging.info(name)

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

TimerTrigger Function Code :-

import datetime
import logging
import requests

import azure.functions as func


def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)

    url = '<My URL>'
    PARAMS = {"name": "Akshay"}

    response_decoded_json = requests.post(url=url, params =PARAMS)
    print(response_decoded_json.text)
    logging.info("Result is :- ")
    logging.info(response_decoded_json.text)

Please check below error log image :-

enter image description here

2
  • Any update on this issue? Commented Dec 7, 2019 at 15:14
  • @GeorgeChen I am facing the The timer is past due! issue in TimerTrigger function. Commented Dec 10, 2019 at 5:21

1 Answer 1

2

Of course this is available, just in your timer trigger function do a get/post request. The below is my test code.

import datetime
import logging
import requests

import azure.functions as func

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    url = 'your function url'
    PARAMS = {"name": "test"}

    response_decoded_json = requests.post(url=url, params =PARAMS)
    print(response_decoded_json.text)

enter image description here

I got the respond from the http trigger function, as for your requirement, change the cron expression to 0 0 21 * * *.

Update: I test this is available.

enter image description here

Timer Function code:

import datetime
import logging
import requests

import azure.functions as func


def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
    url = 'https://functionname.azurewebsites.net/api/httptest?code=master key'
    PARAMS = {"name": "test"}

    response_decoded_json = requests.post(url=url, params =PARAMS)
    logging.info(response_decoded_json.text)

HTTP function code:

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello {name}!")
    else:
        return func.HttpResponse(
             "Please pass a name on the query string or in the request body",
             status_code=400
        )

When upload the function remember add requests==2.22.0 in requirements.txt.

Suppose the main problem is about how to get the httptrigger function url. The url the main parameter you need is the Function APP name, function name and the master key(or default key). The Function APP name and function name actually you already know.

About the code(key), you could deploy any function firstly then, go to the manage page you will be able to get the code and paste after the url you will get the full http trigger url. Then upload the two functions, it will be okay.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.