使用python请求和json后文件

使用python请求和json后文件

问题描述:

我已经给出了下面的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://*.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://*.com/questions/8107177/) – 2012-04-19 20:04:02

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:不是JSON可序列化的”。 – Leah 2012-05-02 00:59:08

+0

显示您的代码。 – 2012-05-02 09:43:39