Search code examples
javahttphttp-status-code-400

HTTP GET request returning 400 response code


I'm working on a simple API for a few projects but when I make a GET request from the application it returns a response code of 400. Any help in fixing the issue will be greatly appreciated.

My Code:

URL oracle = new URL(URL+"SELECT `name`, `username`, `email`, `position`, `status` FROM `"+pl.APIKEY+"` WHERE 1");
            HttpURLConnection con = (HttpURLConnection) oracle.openConnection();
            con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
             con.setRequestMethod("GET");
             con.setDoOutput(false);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));


            String inputLine;
            int size = 0;
            String position=null ,user = null,email= null,name= null;
            int status = 0;
            while (true){
                inputLine = in.readLine();
                if(inputLine == null){
                    break;
                }
                System.out.println(inputLine);
                String[] input = inputLine.split("`");
                user = input[0];
                email = input[1];
                name = input[2];
                position= input[3];
                status = 0;
             }

I've looked around for what I did wrong and found nothing so if you see anything please feel free to say so, thanks again!


Solution

  • I think the problem is in your URL. You should encode this part:

    "SELECT name, username, email, position, status FROM "+pl.APIKEY+" WHERE 1"

    Use URLEncoder for this, for example:

    URL oracle = new URL(URL+ URLEncoder.encode("SELECT `name`, `username`, `email`, `position`, `status` FROM `"+pl.APIKEY+"` WHERE 1"), "UTF-8"));