how to get request body text using bottle?

3z33etm

I'm using bottle to receive POST webhook from bitbucket. The body of the POST contains info about what changed in the repo, etc. I am able to do this fine with @post('/myroute'), however I'm having trouble getting to the actual POST body data text.

here is an image that shows what I'm doing end to end http://i.imgur.com/rWc7Hry.png

When printed to consolerequest.body yields:

StringIO.StringIO instance at 0x7fa13341c4d0

and request.body.dir() yields:

AttributeError: StringIO instance has no attribute 'dir'

I'm wondering how do I get to the actual text of the request body (or inspect the object somehow to find the same)?

the POST request body will look something like this:

http://pastebin.com/SWjLrHig

I've 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<[email protected]>","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 so 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<[email protected]>', 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']

it works as expected (just prints the name 'test')

deets

As the bottle documentation states, the request data is "a file like object". http://bottlepy.org/docs/dev/tutorial.html#the-raw-request-body

So you access the raw body using read().

Also, dir is not a method of objects, it's a freestanding function which you call passing an object.

dir(request.body)

And googling for StringIO should have brought you here: https://docs.python.org/2/library/stringio.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the Body from a request, using httptools?

From Dev

How to get GET request body

From Dev

using request.get file body is not correct

From Dev

How to get started on using Bottle-Cork with MongoDB?

From Dev

EssentialAction: How to Get the Body of a Request without Parsing It

From Dev

How to get response headers and body in dispatch request?

From Dev

bottle request.forms.get returns None

From Dev

How to get the response text from an AJAX request using jQuery

From Dev

How to read JSON request body using Dropwizard

From Dev

Nodejs: How can i simply get the request body using express4?

From Dev

How can I block if a request contains a word in get url or post body using urlrewrite for tomcat

From Dev

How do I make a GET request with JSON in the request body

From Java

React How to get and watch for body text direction

From Dev

How to get the body text of email with imaplib?

From Dev

Using JoddHttp put request to send JSON with the .body() corrupts Chinese text

From Dev

How do I get the raw request body from the Request.Content object using .net 4 api endpoint

From Java

HTTP GET with request body

From Dev

http get request with body

From Dev

Elasticsearch GET request with request body

From Dev

How to get the email body in HTML and TEXT from Exchange using EWS in C#?

From Dev

How to get only Message Body from a GET HTTP request?

From Dev

Node.js - get raw request body using Express

From Dev

Can't get request body with resify using Postman to send data

From Dev

bottle web server: how to route to text file

From Dev

How to reset custom request attribute on each request in Bottle?

From Dev

How to get a SOAP body by using SOAPpy?

From Dev

How to get full post body using symfony?

From Dev

How to get the value of a body tag using javascript?

From Dev

How to make API accept URLs as parameters with GET or POST requests using Bottle-python

Related Related

  1. 1

    How to get the Body from a request, using httptools?

  2. 2

    How to get GET request body

  3. 3

    using request.get file body is not correct

  4. 4

    How to get started on using Bottle-Cork with MongoDB?

  5. 5

    EssentialAction: How to Get the Body of a Request without Parsing It

  6. 6

    How to get response headers and body in dispatch request?

  7. 7

    bottle request.forms.get returns None

  8. 8

    How to get the response text from an AJAX request using jQuery

  9. 9

    How to read JSON request body using Dropwizard

  10. 10

    Nodejs: How can i simply get the request body using express4?

  11. 11

    How can I block if a request contains a word in get url or post body using urlrewrite for tomcat

  12. 12

    How do I make a GET request with JSON in the request body

  13. 13

    React How to get and watch for body text direction

  14. 14

    How to get the body text of email with imaplib?

  15. 15

    Using JoddHttp put request to send JSON with the .body() corrupts Chinese text

  16. 16

    How do I get the raw request body from the Request.Content object using .net 4 api endpoint

  17. 17

    HTTP GET with request body

  18. 18

    http get request with body

  19. 19

    Elasticsearch GET request with request body

  20. 20

    How to get the email body in HTML and TEXT from Exchange using EWS in C#?

  21. 21

    How to get only Message Body from a GET HTTP request?

  22. 22

    Node.js - get raw request body using Express

  23. 23

    Can't get request body with resify using Postman to send data

  24. 24

    bottle web server: how to route to text file

  25. 25

    How to reset custom request attribute on each request in Bottle?

  26. 26

    How to get a SOAP body by using SOAPpy?

  27. 27

    How to get full post body using symfony?

  28. 28

    How to get the value of a body tag using javascript?

  29. 29

    How to make API accept URLs as parameters with GET or POST requests using Bottle-python

HotTag

Archive