1

I'm trying to set up a small Python 3.8 script that can listen for and handle POST requests. I want to listen for a POST from Trello, and then just log the data. Every video or guide I read is showing how to handle POST requests from a HTML form.

Trello Example:

{
   "action": {
      "id":"51f9424bcd6e040f3c002412",
      "idMemberCreator":"4fc78a59a885233f4b349bd9",
      "data": {
         "board": {
            "name":"Trello Development",
            "id":"4d5ea62fd76aa1136000000c"
         },
         "card": {
            "idShort":1458,
            "name":"Webhooks",
            "id":"51a79e72dbb7e23c7c003778"
         },
         "voted":true
      },
      "type":"voteOnCard",
      "date":"2013-07-31T16:58:51.949Z",
      "memberCreator": {
         "id":"4fc78a59a885233f4b349bd9",
         "avatarHash":"2da34d23b5f1ac1a20e2a01157bfa9fe",
         "fullName":"Doug Patti",
         "initials":"DP",
         "username":"doug"
      }
   },
   "model": {
      "id":"4d5ea62fd76aa1136000000c",
      "name":"Trello Development",
      "desc":"Trello board used by the Trello team to track work on Trello.  How meta!\n\nThe development of the Trello API is being tracked at https://trello.com/api\n\nThe development of Trello Mobile applications is being tracked at https://trello.com/mobile",
      "closed":false,
      "idOrganization":"4e1452614e4b8698470000e0",
      "pinned":true,
      "url":"https://trello.com/b/nC8QJJoZ/trello-development",
      "prefs": {
         "permissionLevel":"public",
         "voting":"public",
         "comments":"public",
         "invitations":"members",
         "selfJoin":false,
         "cardCovers":true,
         "canBePublic":false,
         "canBeOrg":false,
         "canBePrivate":false,
         "canInvite":true
      },
      "labelNames": {
         "yellow":"Infrastructure",
         "red":"Bug",
         "purple":"Repro'd",
         "orange":"Feature",
         "green":"Mobile",
         "blue":"Verified"
      }
   }
}
Zander
  • 123
  • 1
  • 2
  • 14
  • 1
    parse it with `json.loads` and log it using the `logging` module. But what have you tried so far? Please include the code you wrote even if it doesn't work so we can see that you have put some effort in it – bb1950328 Jun 16 '20 at 09:29
  • Here's the stackoverflow [link](https://stackoverflow.com/questions/20001229/how-to-get-posted-json-in-flask) that addresses this problem – MockinJay Jun 16 '20 at 09:38
  • https://stackoverflow.com/questions/53348748/how-to-extract-data-from-incoming-http-post-using-python The correct answer helped me – Frankenzilla Dec 29 '22 at 10:46

2 Answers2

6

I would look into using a lightweight web application framework like Flask. Using Flask, you could create a simple server-side script in Python to listen for POST requests and process the data.

Example:

First, install Flask: pip install flask.

Then, here's an example script:

from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['POST'])
def result():
    print(request.data)  # raw data
    print(request.json)  # json (if content-type of application/json is sent with the request)
    print(request.get_json(force=True))  # json (if content-type of application/json is not sent)

Flask contains a development server, but to run it in production, you should consult Flask Deployment Options.

JoshG
  • 6,472
  • 2
  • 38
  • 61
  • Thanks for the help, this mostly worked. However, whenever trying `print(request.json)` it prints None. – Zander Jun 16 '20 at 09:46
  • I'm not sure what the request from Trello looks like. What do you get with just `request.data`? I believe `request.json` will only work if the mime type is application/json. – JoshG Jun 16 '20 at 09:47
  • I included the example request in my question, and I can't include the output of `request.data` in this comment because it would exceed the character limit. – Zander Jun 16 '20 at 09:52
  • You provided the actual data or payload from Trello, but that doesn't show the request. Let me take a look at the docs really quick and get back to you momentarily. – JoshG Jun 16 '20 at 09:54
  • What happens if you try `request.get_json(force=True)`? This appears to skip the content-type requirement. – JoshG Jun 16 '20 at 10:02
  • 1
    Perfect! It prints JSON exactly how I needed it. Thanks for the help! – Zander Jun 16 '20 at 10:10
1

if you want to listen to POST request then you need some kind of webserver

you can get the webserver with python by flask,django or any other frame work another option is to use python library "http.server"

https://github.com/hacker1221/python3-server

here I have made simple Python 3 HTTP server for logging all GET and POST requests

enter image description here