簡體   English   中英

python請求| 構造POST請求體

[英]python request | construct POST request body

我正在嘗試使用python請求構造下面的POST請求。

請求標頭

Host: www.example.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:46.0) Gecko/20100101 Firefox/46.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
X-CSRFToken: LdoMdAvsJ3QeFZ79YK1ZKylohMYCEgif
X-Requested-With: XMLHttpRequest
Content-Type: multipart/form-data; boundary=---------------------------6820904081792922663698482414
Cache-Control: max-age=0
Referer: https://www.example.com/def/
Content-Length: 175
Cookie: HE_UTS_ID_CL=5f0ae4142d484a5dbc6579228be42f9a6bc68c95cdd64156af65fffb49e258d4; HE_UTS_ID_LP="/challenges/"; csrftoken=LdoMdAvsJ3QeFZ79YK1ZKylohMYCEgif; user_tz=Asia/Kolkata; _ga=GA1.2.1255646438.1464624573; lordoftherings="a5dr3g48ag2dg8s2b8r57gkil6ioip74:df99595bff3625e78f4caf1281450faf"; mp_mixpanel__c=6; mp_mixpanel__c3=4136; mp_mixpanel__c4=3974; mp_mixpanel__c5=25; hiring_challenge_anon_visited=visited; mp_4c30b8635363027dfb780d5a61785112_mixpanel=%7B%22distinct_id%22%3A%20%22155026da7bb4b-0b9cbbccc1992-3e6e034e-100200-155026da7bceb%22%2C%22url_path%22%3A%20%22%2Fchallenges%2F%22%2C%22%24initial_referrer%22%3A%20%22%24direct%22%2C%22%24initial_referring_domain%22%3A%20%22%24direct%22%7D; HE_UTS_ID=c5bb2531e6f9465bb0d7115d953bdbe79c17ad924dfb467f9e76fa5c19e373b2
Connection: keep-alive

請求正文

-----------------------------6820904081792922663698482414
Content-Disposition: form-data; name="submit"

True
-----------------------------6820904081792922663698482414--

以下是我現在的代碼。

import requests

url = 'https://www.example.com/adadsdas?modern=true'

headers = {'Host': 'www.abc.com',\
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:46.0) Gecko/20100101 Firefox/46.0',\
'Accept': '*/*',\
'Accept-Language': 'en-US,en;q=0.5',\
'Accept-Encoding': 'gzip, deflate, br',\
'X-CSRFToken': '6jdyxOPzlE4ui1HzNa5RRIYhGAaRtalw',\
'X-Requested-With': 'XMLHttpRequest',\
'Referer': 'https://www.example.com/def/',\
'Content-Length': '175',\
'Content-Type': 'multipart/form-data; boundary=---------------------------6820904081792922663698482414',\
'Cookie': 'HE_UTS_ID_CL=4179383969bd41a489340b5ceeb4cde3225d29cfa7734ea09a8a57b0fbd1e0cd; HE_UTS_ID_LP="/challenges/"; csrftoken=6jdyxOPzlE4ui1HzNa5RRIYhGAaRtalw; user_tz=Asia/Kolkata; _ga=GA1.2.1255646438.1464624573; lordoftherings="a5dr3g48ag2dg8s2b8r57gkil6ioip74:df99595bff3625e78f4caf1281450faf"; mp_mixpanel__c=6; mp_mixpanel__c3=4136; mp_mixpanel__c4=3974; mp_mixpanel__c5=25; hiring_challenge_anon_visited=visited; mp_4c30b8635363027dfb780d5a61785112_mixpanel=%7B%22distinct_id%22%3A%20%22155026da7bb4b-0b9cbbccc1992-3e6e034e-100200-155026da7bceb%22%2C%22url_path%22%3A%20%22%2Fchallenges%2F%22%2C%22%24initial_referrer%22%3A%20%22%24direct%22%2C%22%24initial_referring_domain%22%3A%20%22%24direct%22%7D; HE_UTS_ID=efd6ef54d30f48efbc0be67b2283545daf69267d76e6403595f683a26a5adbf7',\
'Connection': 'keep-alive',\
'Cache-Control': 'max-age=0'}

requests.post(url,headers=headers,files={'submit':(None,'True')})

傳遞文件時我得到的是異常dict,如上所述,構造POST請求的正確方法是什么? 如何構建請求體? 並查看原始請求以檢查正在構建的內容?

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 87, in post
    return request('post', url, data=data, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 276, in request
    prep = req.prepare()
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 224, in prepare
    p.prepare_body(self.data, self.files)
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 369, in prepare_body
    (body, content_type) = self._encode_files(files, data)
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 131, in _encode_files
    body, content_type = encode_multipart_formdata(new_fields)
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/filepost.py", line 73, in encode_multipart_formdata
    content_type = get_content_type(filename)
  File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/filepost.py", line 27, in get_content_type
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
  File "/usr/lib/python2.7/mimetypes.py", line 298, in guess_type
    return _db.guess_type(url, strict)
  File "/usr/lib/python2.7/mimetypes.py", line 114, in guess_type
    scheme, url = urllib.splittype(url)
  File "/usr/lib/python2.7/urllib.py", line 1074, in splittype
    match = _typeprog.match(url)
TypeError: expected string or buffer

您的錯誤是根據2013年使用古老版本的請求1.1.0對此答案的評論,因此pip install -U requests將解決此問題。

要做這個帖子你不需要硬編碼任何東西,你創建一個會話並獲得你需要的任何東西,即csrf令牌和請求將處理其余的事情:

import requests   


headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:46.0) Gecko/20100101 Firefox/46.0',
           'X-Requested-With': 'XMLHttpRequest', "referer": "https://www.hackerearth.com/challenges/"}

with requests.Session() as s:
    s.get("https://www.hackerearth.com")
    headers["X-CSRFToken"] = s.cookies["csrftoken"]
    r = s.post("https://www.hackerearth.com/AJAX/filter-challenges/?modern=true", headers=headers,
               files={'submit': (None, 'True')})
    print(r.json())

三個必要的東西是, X-Requested-With因為它必須是ajax請求, referercsrf令牌。

要訪問原始請求數據,您可以訪問r.request上的屬性:

print(r.request.body)
print(r.request.headers)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM