I am using Node with express for the server and okhttp for Android client. I am trying to send a post request to the server using the code below:
FormBody.Builder bodyBuilder = new FormBody.Builder();
for (String key : bodyParams.keySet())
bodyBuilder.add(key, bodyParams.get(key));
Request request = new Request.Builder()
.url(urlBuilder.build())
.post(bodyBuilder.build())
.build();
mHttpClient.newCall(request).enqueue(callback);
And on the server, I set up express with body parser and else:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
And handles the post request here:
app.post(CONSTANTS.ROUTES.USER.REGISTER, function(req, res, next) {
console.log(req.body);
return;
}
However, the req.body is always undefined, okhttp performs normally with get requests and query parameters but I've tried formBody and
RequestBody.create(JSON, json)
method as suggested in this post, none worked, "req.body" always shows as undefined. Can any one help me with this?
okhttp version: 3.4.1
OS: El Capitan
Node: 6.3.0