简体   繁体   English

如何查看请求数据?

[英]How can see the request data?

this is my code,i use requests.python2.7这是我的代码,我使用 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() .当 Request 与.post().get()一起发送时,它会变成requests.PreparedRequest Unfortunately that doesn't have the unencoded data available anymore.不幸的是,没有可用的未编码data了。

What you can get from a POST response is login_response.request.body , but that has been encoded as form data at this point.您可以从 POST 响应中获得login_response.request.body ,但此时已将其编码为表单数据。

To turn it back into a nice to use dict you can use this:要将它变成一个很好用的 dict 你可以使用这个:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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