3

I'm working on an android application and I am trying to retrieve JSON objects from a web API. I am able to read public endpoints and display the results. However, when I try to read private endpoints, I get an error "Permission Denied" in the console. I have read the API document and it states to put the api key in the header. It gives an example:

# Example with curl

curl -X GET https://wger.de/api/v2/workout/ \

-H 'Authorization: Token ae45ad5c68667c4f3a5bfcd983b629875732ecbb'

This is my post request class:

public void sendPostRequest (String where) {
        URL loc = null;
        HttpURLConnection conn = null;
        InputStreamReader is;
        BufferedReader in;

        try {
            loc = new URL(where);
        }
        catch (MalformedURLException ex) {
            return;
        }

        try {
            conn = (HttpURLConnection)loc.openConnection();
            conn.addRequestProperty("Authorization","my-api-key");
            is = new InputStreamReader (conn.getInputStream(), "UTF-8");
            in = new BufferedReader (is);

            readResponse (in);
        }
        catch (IOException ex) {

        }
        finally {
            conn.disconnect();
        }

    }

I tried to add conn.addRequestProperty("Authorization","my-api-key"); but my application fails when trying to recive the JSON object.

Rock56
  • 31
  • 1
  • 2
  • 1
    it's `conn.setRequestProperty("Authorization","my-api-key");` Also post your error logs. If you are sending a request body you need to set `conn.setDoOutput(true);` – Henry Jan 03 '18 at 02:06
  • Possible duplicate of [Adding header for HttpURLConnection](https://stackoverflow.com/questions/12732422/adding-header-for-httpurlconnection) – Henry Jan 03 '18 at 02:11

0 Answers0