2012-04-18 22 views
1

私はAPIドキュメントの一部として以下のcurlコマンドを与えられました。リクエストライブラリを使用して実装しようとしています。 JSONエンコーダーdoesnのファイルを投稿する)(読み取り私が聞いた pythonリクエストとjsonを使ってファイルを投稿する

import requests 
import json 

_user = 'redacted' 
_password = 'redacted' 
_session = requests.session() 
_server = 'http://some.server.com' 

_hdr = {'content-type': 'application/json', 'accept': 'application/json'} 

_login_payload = { 
    'user': { 
     'email': _user, 
     'password': _password 
    } 
} 
r = _session.post(_server + "https://stackoverflow.com/users/sign_in", data=json.dumps(_login_payload), headers=_hdr) 
print json.loads(r.content) 

_spot_payload = { 
    'spot': { 
     'photo': '@rails.gif', 
     'description': 'asdfghjkl', 
     'location_id': 9, 
     'categories': ['See the Sights',] 
    } 
} 
r = _session.post(_server + '/api/v1/spots', data=json.dumps(_spot_payload), headers=_hdr) 
print json.loads(r.content) 

はあなたが開いている( 'ファイル')を使用することができることを教えてくれますが、:。

curl -v --cookie cookie.txt -X POST -H 'Accept: application/json' -F 'spot[photo]'[email protected]s.png -F 'spot[description]'=spot_description -F 'spot[location_id]'=9 -F 'spot[categories][]'='See the Sights' -F 'spot[categories][]'='Learn Something' http://some.server.com/api/v1/spots 

私のPythonコードは次のようになりますこれほど似ていないし、周りの道もわからない。このコマンドを発行すると

+0

関連:[テキストファイルを投稿するためにはPython-要求ライブラリの使用](http://stackoverflow.com/questions/8107177/) –

答えて

3
C:\>cat file.txt 
Some text. 

は:送信されているもの

C:\>curl -X POST -H "Accept:application/json" -F "spot[photo][email protected]" 
-F "spot[description]=spot_description" http://localhost:8888 

はこのようになります:あなたはカールを見ることができるように

POST/HTTP/1.1 User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2.6 libssh2/1.4.0 Host: localhost:8888 Accept: application/json Content-Length: 325 Expect: 100-continue Content-Type: multipart/form-data; boundary=----------------------------e71aebf115cd

------------------------------e71aebf115cd Content-Disposition: form-data; name="spot[photo]"; filename="file.txt" Content-Type: text/plain

Some text. ------------------------------e71aebf115cd Content-Disposition: form-data; name="spot[description]"

spot_description ------------------------------e71aebf115cd--

Content-Typeセットmultipart/form-data;への要求support送信ファイルでリクエストを送信しますContent-Typeを使用してください。これにはfiles引数を使用する必要があります。

(2.7) C:\>python 
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import requests 
>>> requests.__version__ 
'0.11.1' 
>>> requests.post('http://localhost:8888', files={'spot[photo]': open('file.txt', 'rb')}, data={'spot[description]': 'spot_description'}) 
<Response [200]> 

そして、何送られています次のようになります。

POST http://localhost:8888/ HTTP/1.1 
Host: localhost:8888 
Content-Length: 342 
Content-Type: multipart/form-data; boundary=192.168.1.101.1.8000.1334865122.004.1 
Accept-Encoding: identity, deflate, compress, gzip 
Accept: */* 
User-Agent: python-requests/0.11.1 

--192.168.1.101.1.8000.1334865122.004.1 
Content-Disposition: form-data; name="spot[description]" 
Content-Type: text/plain 

spot_description 
--192.168.1.101.1.8000.1334865122.004.1 
Content-Disposition: form-data; name="spot[photo]"; filename="file.txt" 
Content-Type: text/plain 

Some text. 
--192.168.1.101.1.8000.1334865122.004.1-- 
+0

私がどれほど忘れていたかを指摘していただきありがとうございます)。私はおそらく自分のためにそれを理解することができたはずです。しかし、実生活では実際には動かなかったので、私はあなたに戻ってくるのにこれほど長い時間がかかっていました。 – Leah

+0

問題のエラーは "TypeError:はJSONのシリアライズ可能ではありません。 – Leah

+0

コードを表示します。 –

関連する問題