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

  • Tình trạng: Closed
  • Giải thưởng: $10
  • Các bài thi đã nhận: 5
  • Người chiến thắng: sriharivaila2000

Tóm tắt cuộc thi

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.

Các kĩ năng yêu cầu

Những bài dự thi tốt nhất dự cuộc thi này

Xem thêm bài dự thi

Bảng thông báo công khai

  • KMAhn0210
    Chủ cuộc thi
    • cách đây 2 năm

    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.

    • cách đây 2 năm
  • sriharivaila2000
    sriharivaila2000
    • cách đây 2 năm

    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.

    • cách đây 2 năm
  • sriharivaila2000
    sriharivaila2000
    • cách đây 2 năm

    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

    • cách đây 2 năm
  • AhmedHasif
    AhmedHasif
    • cách đây 2 năm

    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.

    • cách đây 2 năm
  • yassinezero
    yassinezero
    • cách đây 2 năm

    Did you test the solution

    • cách đây 2 năm
  • KMAhn0210
    Chủ cuộc thi
    • cách đây 2 năm

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

    • cách đây 2 năm
  • yassinezero
    yassinezero
    • cách đây 2 năm

    What do you want to request exactly ?

    • cách đây 2 năm

Xem thêm bình luận

Làm thế nào để bắt đầu với cuộc thi

  • Đăng cuộc thi của bạn

    Đăng cuộc thi của bạn Nhanh chóng và dễ dàng

  • Nhận được vô số bài dự thi

    Nhận được vô số Bài dự thi Từ khắp nơi trên thế giới

  • Trao giải cho bài thi xuất sắc nhất

    Trao giải cho bài thi xuất sắc nhất Download File - Đơn giản!

Đăng cuộc thi ngay hoặc tham gia với chúng tôi ngay hôm nay!