简体   繁体   中英

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.

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))

Should you be using below

print login_response.text

instead of this?

 print login_response.request.data

basically you are printing the response content not the request that you have sent?

Else request data itself is available for you since you are passing it with the request so if you want you can print it directly eg

print data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM