How to get a request text for a request using a bottle?

I use a bottle to get a POST-webhook from bitbucket. The POST body contains information about what has changed in the repo, etc. I can do this with @post('/myroute') , however I am having trouble getting the actual text of the POST data.

here is an image that shows what i am doing from end to end http://i.imgur.com/rWc7Hry.png

When printing on the console, request.body displays:

StringIO.StringIO instance at 0x7fa13341c4d0

and request.body.dir() gives:

AttributeError: StringIO instance has no attribute 'dir'

I am wondering how can I get to the actual body text of the request (or check the object anyway to find the same)?

The body of the POST request will look something like this:

http://pastebin.com/SWjLrHig

I also tried request.json (no luck)

any advice?

EDIT: I ended up using this:

 from bottle import get, post, request, run import urllib import json @post('/bitbucket') def postToJSON(): body = request.body.read() body = body.replace("+","").replace("payload=","") parsedBody = urllib.unquote(body).decode('utf8') print parsedBody jsonObj = json.loads(parsedBody) print jsonObj 

Interesting now, parsedBody looks good:

 {"repository":{"website":null,"fork":false,"name":"test","scm":"git","owner":" testName","absolute_url":"/testNameTest/test/","slug":"test","is_private":true},"trunc ated":false,"commits":[{"node":"04554d6980dd","files":[{"type":"modified","file" :"stacker.py"}],"raw_author":"TestName< testName@testName.info >","utctimestamp":" 2015-05-2815:30:03+00:00","author":"testName","timestamp":"2015-05-2817:30:03"," raw_node":"04554d6980dd3c5fe4c3712d95b49fcf9b8da4f4","parents":["7f98b4e7532e"], "branch":"master","message":"foo\n","revision":null,"size":-1}],"canon_url":"htt ps://bitbucket.org","user":"testName"} 

but jsonObj is not very good:

 {u'commits': [{u'node': u'7f98b4e7532e', u'files': [{u'type': u'modified', u'fil e': u'stacker.py'}], u'branch': u'master', u'utctimestamp': u'2015-05-2815:24:50 +00:00', u'author': u'TestName', u'timestamp': u'2015-05-2817:24:50', u'raw_node ': u'7f98b4e7532e02d53d83a29ec2073c5a5eac58c8', u'parents': [u'019e77d2e0d3'], u 'raw_author': u'TestNamer< TestName@TestName.info >', u'message': u'foo\n', u'size' : -1, u'revision': None}], u'user': u'TestName', u'canon_url': u'https://bitbuck et.org', u'repository': {u'website': None, u'fork': False, u'name': u'test', u's cm': u'git', u'absolute_url': u'/ericTest/test/', u'owner': u'TestName', u'slug' : u'test', u'is_private': True}, u'truncated': False} 

however, when I do something like

 print jsonObj['repository']['name'] 

works as expected (just prints the name "test")

+6
source share
1 answer

As indicated in the documentation for the bottle, the request data is a "file similar to an object." http://bottlepy.org/docs/dev/tutorial.html#the-raw-request-body

So, you are accessing the original body using read() .

In addition, dir not an object method, it is a standalone function that you call by passing an object.

 dir(request.body) 

And googling for StringIO was supposed to bring you here: https://docs.python.org/2/library/stringio.html

+5
source

Source: https://habr.com/ru/post/988046/


All Articles