Advertisement
MELAMOURI

Untitled

May 12th, 2023
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. import azure.functions as func
  2. import datetime
  3.  
  4.  
  5. def my_function(req: func.HttpRequest) -> func.HttpResponse:
  6.     # Log the request processing
  7.     log_message = 'Received HTTP request for name and id'
  8.     logging.info(log_message)
  9.  
  10.     # Get the name and id parameters from the request
  11.     name = req.params.get('name')
  12.     id = req.params.get('id')
  13.  
  14.     # If the name or id is not present in the query params, check in the request body
  15.     if not name:
  16.         try:
  17.             req_body = req.get_json()
  18.             name = req_body.get('name')
  19.         except ValueError:
  20.             pass
  21.  
  22.     if not id:
  23.         try:
  24.             req_body = req.get_json()
  25.             id = req_body.get('id')
  26.         except ValueError:
  27.             pass
  28.  
  29.     # If both name and id are present, construct the response message
  30.     if name and id:
  31.         current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  32.         response_message = f"Hello {name}! Your ID is {id}. The current date and time is {current_time}."
  33.         return func.HttpResponse(response_message)
  34.     else:
  35.         error_message = 'Invalid request: name and id are required parameters'
  36.         logging.error(error_message)
  37.         return func.HttpResponse(error_message, status_code=400)
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement