Quick issue solving on GCF/Python (awards the first one who gives me a message that solves the problem )

Contest Brief

I am building a function with Python 3.9
・request.get_json() returns None
・400 error with -H "Content-Type: application/json"
・-d '{"message":"hello"}' doesn't even work

I tried request.json() as well.

What the hell is wrong?

Here is the code

```Python
def hello_world(request):
request_json = request.get_json(force=True)
return request_json['message']
```
$ functions-framework --target hello_world
*Also tried deploying the code on actual project but no luck.

```curl
curl -X POST -d '{"message":"hello"}' "http://localhost:8080/"
#500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

curl -X POST -H "Content-Type: application/json" -d '{"message":"hello"}' "http://localhost:8080/"
#<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

```

I've already built GCF with request.args/"GET" request and it works just fine.
Just strated challenging with POST and got this.

Recommended Skills

Top entries from this contest

View More Entries

Public Clarification Board

  • KMAhn0210
    Contest Holder
    • 2 years ago

    Hi sorry I haven't been able to respond.

    Turned out it was a mixture of a bug in my real code and I think function-framework doesn't automatically let me use .get_json() unless I implicitly use flask and dictate class.

    I decided to try testing without commeting out .get_json in local like

    def func(request):
    request_json = request#.get_json()

    then just call func with json object, taking comment out when deploying worked.

    I awarded @sriharivaila2000 who seemed most enthusiastic even without my full disclosure but I wanna thank you all who leaft any comment.

    • 2 years ago
  • sriharivaila2000
    sriharivaila2000
    • 2 years ago

    Hey please use "request.json()" instead of "request.get_json()" to get the whole json data in form of dictionary. And if you want to get specific data from json then use "request.json.get('valuename')" this will solve your problem . And also if you have a you have any doubts see the link which I have kept in comments below.

    • 2 years ago
  • sriharivaila2000
    sriharivaila2000
    • 2 years ago

    Here is the link https://stackoverflow.com/questions/49010415/flask-request-get-json-returning-none-when-valid-json-data-sent-via-post-reque/49010614#49010614?newreg=66b4b997849d4cc2bb4b4dcccf405a22

    • 2 years ago
  • AhmedHasif
    AhmedHasif
    • 2 years ago

    Sir, can you kindly upload the full source code of this project or at-least the file so I can look at it briefly. There are a lot of things missing on your snippet.

    • 2 years ago
  • yassinezero
    yassinezero
    • 2 years ago

    Did you test the solution

    • 2 years ago
  • KMAhn0210
    Contest Holder
    • 2 years ago

    Thank you just updated the post with the code.
    It is very simple.

    • 2 years ago
  • yassinezero
    yassinezero
    • 2 years ago

    What do you want to request exactly ?

    • 2 years ago

Show more comments

How to get started with contests

  • Post your contest

    Post Your Contest Quick and easy

  • Get tons of entries

    Get Tons of Entries From around the world

  • Award the best entry

    Award the best entry Download the files - Easy!

Post a Contest Now or Join us Today!