使用python API POST(请求库)时Node Express为空体

2024-04-20 07:51:10 发布

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

我在用@ErikRas的初学者工具包

对于下面的代码,我在验证python程序时遇到了问题。在

这是我的Python:

import requests
URL="http://localhost"
PORT="3030"

Session = requests.Session()
Request = Session.post(URL+':'+PORT+'/login', data={'name':'AuthedUserName'})
# (Password to follow proof of concept obviously)

在我的api.js标准我刚拿到的文件:

^{pr2}$

在控制台中我得到{}

我发现了这篇文章: req.body empty on postsPython Post Request Body appears empty in Node server when sent

但正如你所见,我已经在使用bodyparser.json以及bodyparser.urlencoded.extended=正确


Tags: 代码import程序localhosthttpurl工具包port
2条回答
import requests
import json

url = 'http://127.0.0.1'
data={'name':'AuthedUserName'}
headers = {'content-type': 'application/json'}

r = requests.post(url=url, data=json.dumps(data), headers=headers)

好的,所以我将pythons请求与web应用的请求进行了比较,方法是将请求打印到node中的控制台。在

我发现web应用程序的头文件比python请求的请求多。网络应用程序: 引用者:'http://localhost:3001/login' 来源:'http://localhost:3001' 主机:'http://localhost:3001' connection:'关闭'

所以我把这个写在我的标题里,它起作用了!

我想知道哪个header属性是“必需的”,所以我逐渐地取出所有内容,看看这是否破坏了POST请求。在

结果我把所有的东西都弄出来了!我现在用的是:

r = Session.post(URL+':'+PORT+'/login',headers = {}, data={'name':'AuthedUserName'})

就这样!!我想知道为什么headers={}有用,但是我需要继续我的项目!!在

<;<;<;<;<;编辑>;>;>;>

上面的“一半”是对的,因为我的web应用程序使用json,我想使用json,所以我需要做的是将我的头更改为

^{pr2}$

然后使用json.dumps文件只在有效载荷上!

r = session.post('http://'+DB_URL+':3030/sinumecUpdate', headers = headers, data = json.dumps(dataObject))

我也需要退出

app.use(bodyParser.urlencoded({ extended: true }));

从我的节点API中,只使用JSON主体解析器。在

相关问题 更多 >