2016-05-30 7 views
3

私は、pythonリクエストを使用して以下のPOSTリクエストを構築しようとしています。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 
+0

あなたは、実際のリンクを共有することはできますか? –

+0

実際のリンクは 'https:// www.hackerearth.com/challenge /'です。 'https://www.hackerearth.com/AJAX/filter-challenges/?modern = true'へのAJAX POSTコールを行います。このAPIをリクエストボディなしで正常に呼び出せますが、リクエストボディを構築する方法を学びたいと思います。 – g4ur4v

答えて

3

あなたのエラーは2013年からrequestes 1.1.0の古代のバージョンを使用してからこの回答へのコメントのとおりであるので、pip install -U requestsはそれを修正します。

あなたは何をハードコーディングする必要はありませんポストを行うには、セッションを作成し、トークンCSRFをIEと要求は残りの世話をする必要があるものは何でも得る:

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

3つの必要なものをX-Requested-Withは、ajaxリクエストでなければならないため、referercsrfトークンです。生のリクエストデータにアクセスするには

、あなたがr.request上の属性にアクセスすることができます

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

フィールドdictに 'None'を渡すときに動作していないpost要求を指すようにするために、csrfトークンをハードコードしました。私はあなたのコードを使用するときにも同じ例外が発生します。 – g4ur4v

+0

@ g4ur4v、これはほとんどの場合、古いバージョンのリクエストを使用しているため、どのバージョンをインストールしていますか?このコードは2.9.1と2.10.0の両方で完璧に動作します。 –

+0

>>> requests .__ version__' '' 1.1.0'' – g4ur4v

関連する問題