首页 > 解决方案 > 当我从 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();

            });

标签: node.js

解决方案


在 curl 请求中,它应该只是“ http://xyz:3002/api/get ”还是“ http://xyz:3002/api/getData ”?


推荐阅读