'JsonObjectRequest sending empty parameters

I'm trying to send a request with json parameters to my node backend. This is the function in Android:

public boolean checkUser(String user, String passw) {

        final String url = "http://10.0.2.2:3000/";
        final String get = "auth";

        JSONObject paramJson = new JSONObject();

        try {
            paramJson.put("user", "value1");
            paramJson.put("pass", "value2");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        Log.d("JSON", paramJson.toString());

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url + get, paramJson,
                response -> {
                    Toast.makeText(getApplicationContext(),
                            response.toString(),
                            Toast.LENGTH_LONG);
                },
                error -> {
                    Toast.makeText(getApplicationContext(),
                            error.toString(),
                            Toast.LENGTH_LONG);
                });
        queue.add(jsonObjectRequest);
        return false;
    }

However, when I try to get the json values on the backend, I got an empty body.

app.get("/auth", async (req, res) => {
    let user = req.body.user;
    let pass = req.body.pass;
    console.log(req.body);
    if (!user || !pass) {
        res.status(500).send("Missing user or pass");
        return;
    }
    ...
}

So basically I don't know how to access the data, I tried logging the whole request but I can't find the json fields or values. Any help here?

Edit:

I already tried the following:

CustomRequest.java

package com.example.apptonia;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public CustomRequest(int method, String url, Map<String, String> params,
                         Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

MainActivity.java

public boolean checkUser(String user, String passw) {

final String url = "http://10.0.2.2:3000/";
final String get = "auth";

JSONObject paramJson = new JSONObject();

try {
    paramJson.put("user", "value1");
    paramJson.put("pass", "value2");

} catch (JSONException e) {
    e.printStackTrace();
}

Log.d("JSON", paramJson.toString());

Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("user", "value1");
paramMap.put("pass", "value2");

CustomRequest jsObjRequest =
        new CustomRequest(Request.Method.GET, url + get, paramMap,
        response -> {
            Toast.makeText(getApplicationContext(),
                    response.toString(),
                    Toast.LENGTH_LONG);
        },
                error -> {
        Toast.makeText(getApplicationContext(),
                    error.toString(),
                    Toast.LENGTH_LONG);
                });

queue.add(jsObjRequest);
return false;

}

Context: I'm developing a demo project with an API that can authenticate users, so I'm sending a get request with the parameters user/password. If there is a more adequate request to make for this, please let me know. This is a first for me.



Solution 1:[1]

https://johncodeos.com/how-to-make-post-get-put-and-delete-requests-with-retrofit-using-kotlin/

How to send a POST request using volley with string body?

https://www.itsalif.info/content/android-volley-tutorial-http-get-post-put

plese follow above link for post json data

Solution 2:[2]

Try to send post request from postman and if you are getting enpty body even from postman than I think you need to add body parser in your node js application In your node js entry file (like Server.js/app.js)

    var bodyParser = require('body-parser');
    var httpApp = express();
    
    httpApp.use(bodyParser.json({
        limit: '50mb'
    }));
    httpApp.use(bodyParser.urlencoded({
        extended: true,
        limit: '50mb'
    }));
    var httpServer = httpApp.listen(httpPort, function () {
    try {
        Logs.WriteLog("Server started on " + httpPort,
            null, "ServerLogs");
      } catch (e) {}
    });

after that you will able get body in below code.

app.get("/auth", async (req, res) => {
    let user = req.body.user;
    let pass = req.body.pass;
    console.log(req.body);
    if (!user || !pass) {
        res.status(500).send("Missing user or pass");
        return;
    }
    ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 axar
Solution 2