2012-04-18 52 views
1

我已經給出了下面的curl命令作爲API文檔的一部分,並且我試圖使用請求庫來實現它。使用python請求和json後文件

curl -v --cookie cookie.txt -X POST -H 'Accept: application/json' -F 'spot[photo]'[email protected] -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代碼看起來是這樣的:

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) 

我聽說告訴你可以使用open(「文件」)閱讀()發佈的文件,但JSON編碼器沒有按這很像,我不確定解決方法。

+0

相關:使用Python,請求圖書館發佈文字文件](http://stackoverflow.com/questions/8107177/) – 2012-04-19 20:04:02

回答

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 2012-05-02 00:49:12

+0

有問題的錯誤是「TypeError:<打開文件'logo.gif',模式'rb'在[hex here]>不是JSON可序列化的」。 – Leah 2012-05-02 00:59:08

+0

顯示您的代碼。 – 2012-05-02 09:43:39