Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can see the request data?

this is my code,i use requests.python2.7

 #create requests
    requests_vivo = requests.Session()
    #login url
    login_url = 'https://id.vivo.com.cn/api/login'
    #captcha_url
    captcha_url = 'https://id.vivo.com.cn/api/kaptcha.jpg?t=%.0f' % time.time()
    #header
    header = {
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Encoding": "gzip,deflate,sdch",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Host": "id.vivo.com.cn",
        "Origin": "https://id.vivo.com.cn",
        "Referer": "https://id.vivo.com.cn/?_%.0f"%time.time(),
        "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
    }
    #request captcha
    captcha_response = requests_vivo.get(url=captcha_url,headers=header)
    #write jpg
    with open('captcha_pic.jpg','wb') as f:
        f.write(captcha_response.content)

    captcha_code = raw_input('Please input code:')

    #data
    data = {
        "name": setting.username,
        "password": encryptPasswd(setting.password),
        "verificationCode": captcha_code,
        "remember": "0"
    }

    #login request
    login_response = requests_vivo.post(url=login_url,headers=header,data=data)
    print login_response.request.data

this is error,i can't see data:

#captcha
Please input code:8men
8men

    #error info
    Traceback (most recent call last):
      File "/home/freedom/work/app/sem/vivo/test.py", line 39, in <module>
        print login_response.request.data
    AttributeError: 'PreparedRequest' object has no attribute 'data'

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.

like image 860
王胖胖 Avatar asked Oct 29 '25 14:10

王胖胖


1 Answers

The Request is turned into a requests.PreparedRequest when it is sent with .post() or .get(). Unfortunately that doesn't have the unencoded data available anymore.

What you can get from a POST response is login_response.request.body, but that has been encoded as form data at this point.

To turn it back into a nice to use dict you can use this:

# py2
import urlparse
dict(urlparse.parse_qsl(login_response.request.body))

or

# py3
from urllib.parse import parse_qsl
dict(parse_qsl(login_response.request.body))
like image 104
rdrey Avatar answered Oct 31 '25 03:10

rdrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!