私は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コードは次のようになりますこれほど似ていないし、周りの道もわからない。このコマンドを発行すると
関連:[テキストファイルを投稿するためにはPython-要求ライブラリの使用](http://stackoverflow.com/questions/8107177/) –