3

I wants to use python requests module to prepare http request and use http socket to send the request, So I can detect the connection problem. Can anyone suggest how should I retrieve raw request from python requests module(without sending request)

from requests import Request, Session
for m in ['GET', 'POST']:
    s = Session()
    req = Request(m, url,
        data=data,
        headers=header
    )
    prepped = req.prepare()
    # how to retrieve raw request from prepped 
user87005
  • 964
  • 3
  • 10
  • 27
  • http://stackoverflow.com/questions/20658572/python-requests-print-entire-http-request-raw/23816211#23816211 – Ajay Jun 01 '15 at 13:50
  • 1
    I have already seen the same page, But this is not generic for different htt method like GET doesn't have body. – user87005 Jun 02 '15 at 06:56

1 Answers1

0

prepped (PreparedRequest Object) has:

  • Attributes:

body : request body to send to the server.

url: HTTP URL to send the request to.

headers : dictionary of HTTP headers.

hooks : dictionary of callback hooks, for internal usage.

method : HTTP verb to send to the server.

path_url: Build the path URL to use.

  • Methods:

deregister_hook(event, hook): Deregister a previously registered hook. Returns True if the hook existed, False if not.

prepare(method=None, url=None, headers=None, files=None, data=None, params=None, auth=None, cookies=None, hooks=None, json=None): Prepares the entire request with the given parameters.

prepare_auth(auth, url=''): Prepares the given HTTP auth data.

prepare_body(data, files, json=None): Prepares the given HTTP body data.

prepare_cookies(cookies):Prepares the given HTTP cookie data.

prepare_headers(headers): Prepares the given HTTP headers.

prepare_hooks(hooks): Prepares the given hooks.

prepare_method(method): Prepares the given HTTP method.

prepare_url(url, params): Prepares the given HTTP URL.

register_hook(event, hook): Properly register a hook.

You can access them prepped.what_you_want.

Reference

Yahya Yahyaoui
  • 2,833
  • 23
  • 30