Python中带有POST请求的json数据没有arri

2024-10-03 21:24:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我喜欢构建一个小型的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,那么效果很好。有人知道为什么吗?在


Tags: 代码importjsonurldataapplicationlocaltype
1条回答
网友
1楼 · 发布于 2024-10-03 21:24:25

在PHP中$_POST只包含表单数据(application/x-www-form-urlencodedmultipart/form-data编码的POST body)。在

要获取JSON数据,您需要直接使用^{}读取请求正文:

$json = json_decode(file_get_contents("php://input"));

相关问题 更多 >