3

I am trying to make a GET request to a local server I have running. I am having trouble returning the correct data, I am seeing an 'Unauthorized' response. Can anyone spot any glaring issues with this given that the String 'token' is correct.

    protected Object doInBackground(Void... params) {
        try {
            String url = "http://192.168.0.59:8000/events/";
            URL object = new URL(url);
            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestMethod("GET");
            con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestProperty("Authorization:", "Token " + token);

            //Display what the GET request returns
            StringBuilder sb = new StringBuilder();
            int HttpResult = con.getResponseCode();
            if (HttpResult == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(con.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
            } else {
                System.out.println(con.getResponseMessage());
            }
        } catch (Exception e) {
            Log.d("Uh Oh","Check your network.");
            return false;
        }
        return false;

}*

I was able to get a curl request working from the command line:

curl -H "Authorization: Token token" http://0.0.0.0:8000/events/
David Lam
  • 4,689
  • 3
  • 23
  • 34
Ron95
  • 136
  • 1
  • 2
  • 13
  • You need to authenticate yourself. – Somnath Musib Oct 25 '17 at 00:50
  • see https://stackoverflow.com/questions/12732422/adding-header-for-httpurlconnection – Scary Wombat Oct 25 '17 at 00:53
  • 1
    Drop the `:` in `"Authorization:"`. – xiaofeng.li Oct 25 '17 at 01:15
  • As @xiaofeng.li correctly pointed out, you have unnecessary `:` in header name, should be like this : `con.setRequestProperty("Authorization", "Token " + token);` – rkosegi Oct 25 '17 at 06:46
  • @rkosegi Thanks for the advice, when I include the colon I get a "Unauthorized" response, and without it I get an "Internal Server Error" response. – Ron95 Oct 25 '17 at 16:02
  • @ConorNaylor `Internal Server Error` means there's something wrong with the server. It also means you passed authentication. – xiaofeng.li Oct 25 '17 at 22:33
  • @xiaofeng.li Again, I appreciate the help, I actually managed to find the answer in this post: https://stackoverflow.com/questions/8760052/httpurlconnection-sends-a-post-request-even-though-httpcon-setrequestmethodget – Ron95 Oct 25 '17 at 22:59

2 Answers2

3

try this con.setRequestProperty("Authorization", "Bearer " + token);

2

It turns out this issue was caused by including the con.setDoOutput(true); as get requests do not include a body.

Ron95
  • 136
  • 1
  • 2
  • 13