Result: Failure Exception: AttributeError: 'str' object has no attribute 'get' at project_id = req_body.get("project_id", None) this

Gurram, Munisekhar 20 Reputation points
2023-07-10T08:08:11.0966667+00:00
def main(req: func.HttpRequest) -> func.HttpResponse:



try:
        req_body = req.get_json()
        print(req_body)
        project_id = req_body.get("project_id", None)
        print(project_id)
    except ValueError:
        filterdata = {}
    else:
        update_flag = req_body.get("update", 0)
        filterdata = req_body.get("filterdata", {})

Result: Failure Exception: AttributeError: 'str' object has no attribute 'get' Stack: File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 479, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 752, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/optimize_spi/init.py", line 56, in main project_id = req_body.get("project_id", None)

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,408 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pramod Valavala 20,656 Reputation points Microsoft Employee Moderator
    2023-07-10T19:10:03.4233333+00:00

    @Gurram, Munisekhar Based on the error message, looks like the req_body is a string, likely because the JSON payload is double serialized.

    You either must fix this in the client or add a check like this to make sure it is a dictionary

    import json
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        try:
            req_body = req.get_json()
            print(req_body)
            if isinstance(req_body, str):
                req_body = json.loads(req_body)
            project_id = req_body.get("project_id", None)
            print(project_id)
        except ValueError:
            filterdata = {}
        else:
            update_flag = req_body.get("update", 0)
            filterdata = req_body.get("filterdata", {})
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.