0

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

Community
  • 1
  • 1
TPWang
  • 1,322
  • 4
  • 20
  • 39
  • Rather you should use Android Volley for Http request in android: https://developer.android.com/training/volley/simple.html – Akhter Al Amin Jul 18 '16 at 07:22
  • Try this link : Feel free to ask if help required.. [OKHttp post implementation](http://stackoverflow.com/questions/28135338/okhttp-library-networkonmainthreadexception-on-simple-post/38244995#38244995) – Maddy Sharma Jul 18 '16 at 07:23
  • I would stick with this for now unless it is proven that okhttp does not handle post request, and the link provided was not helpful to my situation – TPWang Jul 18 '16 at 07:29

0 Answers0