【问题标题】:post-ing a file using python requests and json使用 python 请求和 json 发布文件
【发布时间】:2012-04-18 01:23:05
【问题描述】:

作为 API 文档的一部分,我得到了以下 curl 命令,我正在尝试使用 requests 库来实现它。

curl -v --cookie cookie.txt -X POST -H 'Accept: application/json' -F 'spot[photo]'=@rails.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 代码如下所示:

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 + "/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('file').read() 来发布文件,但是 json 编码器不太喜欢这样,我不确定是否有解决方法。

【问题讨论】:

标签: python json post curl python-requests


【解决方案1】:
C:\>cat file.txt
Some text.

当你发出这个命令时:

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

发送的内容如下所示:

POST / HTTP/1.1 用户代理:curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2.6 libssh2/1.4.0 主机:localhost:8888 接受: 应用程序/json 内容长度:325 预期:100-继续 内容类型:multipart/form-data; 边界=----------------e71aebf115cd

------------------e71aebf115cd 内容处置:表单数据;名称=“地点[照片]”; filename="file.txt" 内容类型: 文本/纯文本

一些文字。 ------------------------------e71aebf115cd 内容处置:表单数据;名称=“地点[描述]”

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

如您所见,curl 发送请求时将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--

【讨论】:

  • 感谢您指出我是多么的健忘 =)。我可能应该能够自己弄清楚这一点。但是,我花了这么长时间才回复您,因为它在现实生活中实际上并没有起作用,而且我花了一段时间才发现有用的错误。
  • 有问题的错误是“TypeError: is not JSON serializable”。
  • 我发现中间代码中存在问题,我将文件发送到数据而不是文件。服务器仍然没有正确接收文件,但是 python 不再抛出任何错误。恐怕出于保密原因,我无法分享实际的代码/服务器地址。我的下一步是找到一个工具来捕获从我的 mac 到服务器的网络流量,这样我就有了一些具体的东西可以给开发人员。感谢您的所有帮助。
  • 也许你知道为什么用 json 编码数据失败:data=json.dumps({'spot[description]': 'spot_description'})
  • @AlexKreimer 您应该将此作为单独的问题提出。还要确保它尚未被询问。
猜你喜欢
  • 2013-10-26
  • 2020-12-11
  • 1970-01-01
  • 2017-08-31
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多