我喜欢构建一个小型的REST接口来连接PYTHON和PHP。 在谷歌上了几个小时之后,我几乎从几个讨论板上复制和粘贴代码:
import requests
import json
url = 'http://spidercontrol.ilumiweb.local/helge/voltage'
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
data = {'value': '7.4', 'decay_time': '300'}
r = requests.post(url, data=json.dumps(data), headers=headers)
这应该将数据作为json通过POST发送到给定的网址。这个发送到的位置:
^{pr2}$但是$后的数据中没有:
Method: POST
Array
(
[Host] => spidercontrol.ilumiweb.local
[Content-Length] => 37
[Content-type] => application/json
[Accept-Encoding] => gzip, deflate, compress
[Accept] => text/plain
[User-Agent] => python-requests/2.2.1 CPython/2.7.6 Windows/7
)
Post:Array
(
)
Get:Array
(
)
如果我使用相同的代码,但是删除headers=headers information,但是data=data,那么效果很好。有人知道为什么吗?在
在PHP中
$_POST
只包含表单数据(application/x-www-form-urlencoded
或multipart/form-data
编码的POST body)。在要获取JSON数据,您需要直接使用^{} 读取请求正文:
相关问题 更多 >
编程相关推荐