Python Requests - Print Entire Http Request (Raw)

Print out the whole raw http request

As far as I can tell from the docs you can't get the data in raw format.

What you can do is reconstruct it using bottle.request.data and bottle.request.headers. That may be enough for your purposes.

How to get dynamic choices field in admin panel of django- category sub-category

Doing this is possible when you have UI control.
I would suggest first having your custom JS in the Admin Panel to Add Field and its choices based on the value of category field.

To add choices to the Admin Panel you can add choices attribute in your model field.

product_type = [('shoes','Shoes'),('watches','Watches')]

class categorie(models.Model):
name = models.CharField(max_length=100, unique=True,choices=product_type)

Now you need to add js to Admin Panel :How to load Custom JS in Django Admin Home
You can try this or a similar approach and check for the value of the field categorie in your JS and when it changes to lets say 'Shoes' then you need to add the field sub category with choices in select as 'Adidas,Puma,Nike,Reebok etc'.


UPDATE 1:
How to add custom JS in particular field:
When you have model in your app ,lets take the same categorie you have name as your field.You go to the ADMIN PANEL for this model, your field will have id as id_name as default.So your input will look something like.

<input type="text" name="name" class="vTextField" maxlength="100" id="id_name">

So you now have the id of your field in your admin forms.So for every fieldname you have id as id_fieldname.
You can add an Event Listener in JS for that field on value change.

document.getElementById('id_fieldname').addEventListener('change',function(){
#Do what you want here
});

So in that function either you can call another function that adds the select input field which have the required options or have the field already there and just insert options in the select field.

How to get the raw content of a response in requests with Python?

If you are using a requests.get call to obtain your HTTP response, you can use the raw attribute of the response. Here is the code from the requests docs. The stream=True parameter in the requests.get call is required for this to work.

>>> r = requests.get('https://github.com/timeline.json', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

How can I see the entire HTTP request that's being sent by my Python application?

A simple method: enable logging in recent versions of Requests (1.x and higher.)

Requests uses the http.client and logging module configuration to control logging verbosity, as described here.

Demonstration

Code excerpted from the linked documentation:

import requests
import logging

# These two lines enable debugging at httplib level (requests->urllib3->http.client)
# You will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# The only thing missing will be the response.body which is not logged.
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

requests.get('https://httpbin.org/headers')

Example Output

$ python requests-logging.py 
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): httpbin.org
send: 'GET /headers HTTP/1.1\r\nHost: httpbin.org\r\nAccept-Encoding: gzip, deflate, compress\r\nAccept: */*\r\nUser-Agent: python-requests/1.2.0 CPython/2.7.3 Linux/3.2.0-48-generic\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Type: application/json
header: Date: Sat, 29 Jun 2013 11:19:34 GMT
header: Server: gunicorn/0.17.4
header: Content-Length: 226
header: Connection: keep-alive
DEBUG:requests.packages.urllib3.connectionpool:"GET /headers HTTP/1.1" 200 226

Raw request and response to HTTPRequest and HTTPResponse, respectively

Parsing is done in https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/net/http/http1/read.py and https://github.com/mitmproxy/mitmproxy/blob/master/mitmproxy/net/http/http1/read_sansio.py. Note that the latter will be merged in the former in a few days (we're just in the middle of a major restructuring) and some of the methods currently in read.py will disappear.

How to extract HTTP response body from a Python requests call?

Your code is correct. I tested:

r = requests.get("http://www.google.com")
print(r.content)

And it returned plenty of content.
Check the url, try "http://www.google.com". Cheers!



Related Topics



Leave a reply



Submit