Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting json input in `req.get_json()` in azure function app in python

I have created few apis before in azure function app. I had used req.get_json() to get the json input parameter but suddenly it stopped. The value of req.get_json() is giving me error ValueError: HTTP request does not contain valid JSON data. I tried following basic code sample. Its giving me the same error.

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}. 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
        )

curl command:

curl --location --request POST 'http://localhost:7071/api/dev-test' \
> --header 'Content-Type: application/json' \
> --data-raw '{"name":"test-dev"}​'

output of curl:

This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
like image 916
Komal Nagada Avatar asked Oct 29 '25 01:10

Komal Nagada


1 Answers

I have faced same issue before, this is mostly I think postman's issue. You can download postcode extension for vscode from marketplace and test using it.

In the curl command you might also have issue as mentioned by Frank above. To remove that issue, you can instead create one JSON file and paste your json data in it and then try with curl like: curl --location --request POST "http://localhost:7071/api/dev-test" --header 'Content-Type: application/json' -d @test.json where test.json will contain your json data. It may or may not work.

like image 98
Atul Singh Avatar answered Oct 31 '25 02:10

Atul Singh