node.js - 当我从 Node 服务器访问后端时,为什么会收到 404 Bad request?
问题描述
React 正在调用 Node API,Node 正在与后端微服务交互。我正在发出一个 POST 请求,其中授权令牌需要成为标头的一部分,并且还需要知道用户名。
我在下面提供了我的代码,但找不到我收到 400 - 错误请求的原因,因为我没有发布任何可能超过服务器容量的文件。非常感谢您的帮助。
这是我的请求应该是这样的:
curl -d '{"userName": "Tom and Jerry"}' -H "Content-Type: application/json" -H "Authorization: Bearer dwgqhsfjnfjjfldklkfldskglkylrkylktyl" -X POST http://nodejs-appfactory1:25002/appurl-service/api/appurl/getClientList
这是我在 Node 中的请求:
app.post('/getData', async (req, res) => {
var getToken = (authorization_token) => {
return prop.get(authorization_token);
};
var authorization_token = getToken('authorization_token');
var post_data = querystring.stringify({
'userName': req.session.user
});
var options = {
host: 'nodejs-appfactory1',
port: 25002,
path: 'appurl-service/api/appurl/getClientList', // the rest of the url with parameters if needed
method: 'POST',
headers: {
'Authorization': authorization_token
}
};
var post_req = http.request(options, function (resp) {
var responseString = "";
resp.on("data", function (data) {
responseString += data;
console.log("Success ---- Response from server:" +responseString);
res.json(responseString);
// save all the data from response
});
resp.on("end", function () {
console.log("Error ---- Response from Server:" +responseString);
// print to console when response ends
});
});
post_req .write(post_data);
post_req .end();
});
解决方案
在 curl 请求中,它应该只是“ http://xyz:3002/api/get ”还是“ http://xyz:3002/api/getData ”?
推荐阅读
- java - 我们可以将“return”替换为“throw new Exception”吗?
- graphql - GraphQL 中的复杂查询变量(通过 Gatsby)
- ruby-on-rails - Rails active_storage:install 不会安装
- php - 单击图像时,它不会在 jQuery 中删除
- python-3.x - 抓取内部 div 的文本
- c# - 如何使用列表和 sql 数据库列动态创建按钮 mvc
- python - AttributeError:模块“sqlalchemy.util”没有属性“deprecated_params”
- javascript - 为什么只有第一个值被写入变量
- xaml - Xamarin Forms 的 Windows Phone StackPanel 控件替代方案?
- plantuml - 使用标签更改思维导图对象的背景颜色