简体   繁体   English

当使用httpPost从java发送时,node.js req.body为空

[英]node.js req.body empty when sending from java with httpPost

When I'm trying to send some json to my node.js server req.body is empty and I have no idea why. 当我试图将一些json发送到我的node.js服务器时,req.body是空的,我不知道为什么。 The headers I'm sending are received. 收到我正在发送的标题。

This is the java code: 这是java代码:

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        StringEntity se = new StringEntity(json, "UTF8");

        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("test", "test");

        HttpResponse httpResponse = httpClient.execute(httpPost);

And this is the node.js code: 这是node.js代码:

exports.checkMail = function(req, res) {
    var user = req.body;
    var header = req.headers;
    var email = user.email;
    db.collection('users', function(err, collection) {
        collection.findOne({'email':email}, function(err, item) {
            if (item) {
                res.send({'error':0, 'message':'email available'});
            } else {
                res.send({'error':4, 'message':'email already taken'});
            }
        });
    });
};

Any ideas of what I might be missing? 我可能会缺少什么想法?

EDIT: 编辑:

Sorry forgot to mention. 对不起忘了提。 I'm using Express. 我正在使用Express。 And the json i'm sending is: 而我发送的json是:

{"email":"email@email.com"}

What I get from req.body is just: {} 我从req.body得到的只是:{}

EDIT2: EDIT2:

app.js app.js

    var express = require('express'),
        post = require('./routes/posts'),
        user = require('./routes/user');

    var app = express();

    app.configure(function () {
        app.use(express.logger('dev'));
        app.use(express.bodyParser());
        //app.use(express.json());
        //app.use(express.urlencoded());
    });

    app.get('/auth',            user.login); 
    app.post('/auth',           user.addUser);
    app.put('/auth/:id',        user.updateUser); 
    app.post('/checkmail',      user.checkMail);  
    app.post('/reset',          user.resetPassword); 

    app.listen(8080);
    console.log('Listening on port 8080...');

Probably too late to answer, but if anybody has this same problem this is what worked for me: 回答可能为时已晚,但如果有人遇到同样的问题,这对我有用:

//It is important that if we are sending a Content-Type application/json, the entity has to receive a json 
JSONObject js = new JSONObject();
js.put("key", value);
String url = "http://mylocalhosturl";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(js.toString(), HTTP.UTF_8);

//Setting the content type is very important
entity.setContentEncoding(HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);

//Execute and get the response.
HttpResponse response = httpClient.execute(httpPost);

Hope this helps to someone. 希望这对某人有帮助。

You app.js file should have these middleares included; app.js文件应包含这些中间值;

var express = require('express');

var app = express();

// app configs; include these;
app.use(express.json());
app.use(express.urlencoded());

// other configurations

app.use(app.router)

// ...

Hope this helps. 希望这可以帮助。

I would first determine if the problem is client side or server side. 我首先要确定问题是客户端还是服务器端。 Your client code looks ok to me, so I would try hitting your server with a simple curl command. 您的客户端代码对我来说没问题,所以我会尝试使用简单的curl命令来命中您的服务器。

For example: 例如:

curl -k -i -H 'Accept: application/json' -H 'Content-type: application/json' -X POST -d '<YOUR POST BODY>' 'http://yourhost/yourendpoint'

This will help determine if it's a purely server side issue, which I suspect in this case it is. 这将有助于确定它是否是纯粹的服务器端问题,我怀疑在这种情况下它是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM