0

I have this piece of code it is a post method (Python Flask),

@application.route('/try', methods=['POST'])
def tryPost() :
    content = "```" + str(request.json) + "```"
    return jsonify(content)

When i run the app locally and post json on postman to the http://127.0.0.1:5000/try , i can see the same json result.

But after i push my app to the aws elastic beanstalk, and i posted the same method it returned "```None```"

What would be the reason?

This the example json

{
    "api_app_id": "A02",
    "token": "Shh_its_a_seekrit",
    "container": {
        "type": "message",
        "text": "The contents of the original message where the action originated"
    }
}
Sevval Kahraman
  • 1,185
  • 3
  • 10
  • 37

1 Answers1

1

request.json only works when you send a request with Content-Type of application/json. Otherwise is None.

Without the content-type, you should use request.data:

@application.route('/try', methods=['POST'])
def tryPost() :
    content = "```" + str(request.data) + "```"
    return content
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Thank you @Marcin, but now it returns like this, how can i fix this? ; b'{\r\n\t"api_app_id": "A02",\r\n\t"token": "Shh_its_a_seekrit",\r\n\t"container": {\r\n\t\t"type": "message",\r\n\t\t"text": "The contents of the original message where the action originated"\r\n\t}\r\n}' – Sevval Kahraman Feb 13 '21 at 11:40
  • 1
    @ŞevvalKahraman I think you have to just return `request.data`, without `str` operation. – Marcin Feb 13 '21 at 11:43
  • but then i cannot concatenate with other strings :/ – Sevval Kahraman Feb 13 '21 at 11:45
  • @ŞevvalKahraman Sorry, I don't know what do you want to achieve. Why not pass Content-Type of application/json with your request? – Marcin Feb 13 '21 at 11:47
  • 1
    Thank you @Marcin, i wanted to show this requests on slack. I returned that str(request.data, "utf-8") and it works. – Sevval Kahraman Feb 13 '21 at 11:56