JSON Express 服务器中的请求正文始终为空
JSON Request body always empty in Express server
我正在用 express 构建一个小型服务器和网站。
现在我想用编码为 JSON 的简单凭据向我的快速服务器发送一个 POST 请求。
我的问题是无论我尝试什么,我的请求正文在服务器端总是空的。我确定在前端有值,我的代码 console.logs 在发送请求之前是正确的用户名和密码。我可以在 chrome 开发工具的网络面板中看到发送到服务器的正确负载
前端代码:
<script>
const username_input = document.getElementById('username');
const password_input = document.getElementById('password');
const submit_registration = document.getElementById('submit_registration');
submit_registration.addEventListener('click', (event) => {
const username = username_input.value;
const password = password_input.value;
console.log(username, password);
fetch('/register', {
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
'username': username,
'password': password
})
})
.then(raw_response => raw_response.json())
.then(response => console.log(response))
.catch(err => console.log(err))
});
</script>
我还尝试向我的服务器发送一个简单的 POST 请求。我使用的卷曲:
curl -H POST "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/register
我的服务器代码中的请求正文再次为空。
我猜问题出在后端的某个地方,但我不确定,所以我通过 curl 添加请求并获取以确保。
这是我处理 POST 请求的快速代码:
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.post('/register', (req, res) => {
console.log(req.body);
res.status(200).send('Ok');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
结果始终是空的大括号,但假设其中包含用户名和密码,就像在上面的请求中发送一样,我不明白为什么它总是空的。
我的 Express 版本是 4.17,所以 express.json() 应该可以工作。
我必须补充一点,当我使用 html 表单并通过 application/x-www-form-urlencoded 编码数据并在我的 express 应用程序中使用
对其进行解码时
app.use(express.urlencoded({ extended: true}))
它起作用了。我收到了用户名和密码,但现在 json 后端的正文总是空的。
我很沮丧
试试 res.status(200).json('Ok');
将提取请求中的 header
属性 更改为 headers
。
我正在用 express 构建一个小型服务器和网站。 现在我想用编码为 JSON 的简单凭据向我的快速服务器发送一个 POST 请求。 我的问题是无论我尝试什么,我的请求正文在服务器端总是空的。我确定在前端有值,我的代码 console.logs 在发送请求之前是正确的用户名和密码。我可以在 chrome 开发工具的网络面板中看到发送到服务器的正确负载 前端代码:
<script>
const username_input = document.getElementById('username');
const password_input = document.getElementById('password');
const submit_registration = document.getElementById('submit_registration');
submit_registration.addEventListener('click', (event) => {
const username = username_input.value;
const password = password_input.value;
console.log(username, password);
fetch('/register', {
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
'username': username,
'password': password
})
})
.then(raw_response => raw_response.json())
.then(response => console.log(response))
.catch(err => console.log(err))
});
</script>
我还尝试向我的服务器发送一个简单的 POST 请求。我使用的卷曲:
curl -H POST "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/register
我的服务器代码中的请求正文再次为空。 我猜问题出在后端的某个地方,但我不确定,所以我通过 curl 添加请求并获取以确保。 这是我处理 POST 请求的快速代码:
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.post('/register', (req, res) => {
console.log(req.body);
res.status(200).send('Ok');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
结果始终是空的大括号,但假设其中包含用户名和密码,就像在上面的请求中发送一样,我不明白为什么它总是空的。 我的 Express 版本是 4.17,所以 express.json() 应该可以工作。
我必须补充一点,当我使用 html 表单并通过 application/x-www-form-urlencoded 编码数据并在我的 express 应用程序中使用
对其进行解码时app.use(express.urlencoded({ extended: true}))
它起作用了。我收到了用户名和密码,但现在 json 后端的正文总是空的。
我很沮丧
试试 res.status(200).json('Ok');
将提取请求中的 header
属性 更改为 headers
。