0

I can't figure out how to access URL query parameters from a POST request in a Python Azure Function. I am locally testing the function on VSCode.

I've tried:

  1. req.params.get('paramName') - it always is none

  2. url = req.url

    query_parameters = urllib.parse.parse_qs(url.query)

    param = query_parameters.get('paramName', [None])[0]

    • I get the error: url has no query attribute
  3. I also tried os.environ as I saw somewhere that params are automatically set as environment variables for azure functions

1
  • req.params.get('paramName') is what works for python. Maybe we can help better if you can update the question with your actual code and the structure of your API URL. Commented Sep 2, 2023 at 6:12

1 Answer 1

0

I am able to get the query parameter using urllib and req.params.get in the following way

Code:

import  logging
import  azure.functions  as  func
from  urllib.parse  import  urlparse  

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

#using urllib
query_param=  urlparse(req.url).query.split('=')[1]
print("Query Parameter using Urllib:",query_param)  

name  =  req.params.get('name')
print("Query Parameter using 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}. This HTTP triggered function executed successfully.")
else:

return  func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)

Output:

enter image description here

enter image description here

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

Comments

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.