Python Azure function app error 'FunctionBuilder' object has no attribute 'HttpResponse'

Vikrant 20 Reputation points
2024-07-02T02:54:33.0933333+00:00

Hello All

i am trying to setup a azure function app in python 3.11 but getting some error while executing.

Below are the function app configuration details.

Runtime --> python 3.11

Plan --> Consumption

Here is my python code

import azure.functions as func

import logging

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route(route="func", methods=['POST'])

def func(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')

try:

if name:

logging.info(f"Hello, {name}. This HTTP triggered function executed successfully.")

return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")

else:

logging.info("This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.")

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

)

except Exception as e:

logging.info(f"error: {e}")

Logs:image

Reqirenment.txt:

imageasd

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,644 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 20,560 Reputation points Microsoft Employee
    2024-07-09T07:35:16.2833333+00:00

    @Vikrant I'm glad to see you were able to resolve your issue. Thanks for posting your solution so that others experiencing the same thing can easily reference this. Since the Microsoft Q&A community has a policy that the question author cannot accept their own answer, they can only accept answers by others, I'll repost your solution in case you'd like to Accept the answer.

    . Issue:

    Your python based Azure Function App encounters the below error:

    Error 'FunctionBuilder' object has no attribute 'HttpResponse'

    . .

    Resolution:

    You have updated return function as below and that works fine.

    return "Invalid JSON in request body."

    0 comments No comments