4

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.
2
  • What is the output when you use req.get_body()? Commented Jun 10, 2021 at 11:45
  • 1
    b'{"name":"name"}\xe2\x80\x8b' this is the output of req.get_body() Commented Jun 10, 2021 at 12:24

2 Answers 2

2

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.

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

Comments

1

This may be due to the introduction of extra characters when copying. You can delete that line of parameters and manually enter the request parameters. There should be no more problems.

Enter this line manually:

--data-raw '{"name":"test-dev"}​'

3 Comments

@KomalNagada. These characters are invisible, and you'd better enter them character by character instead of just checking them.
I checked by entering manually its not working.
I checked through Postman and created a python client to check but both are also giving same error and same data in req.get_body().

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.