Niels Vanwingh
Niels Vanwingh

Reputation: 604

Check API Request body

I am performing an HTTP Post Request on an API EndPoint. It is the password authorization grant of the OAuth2 protocol.

I get back a 400 code, "bad request".. To advance, I want to get the exact call I am making in a Log.d statement.. My question is how can I log exactly what's in the POST request I am making? It is unclear to me how to do so.. This way I can start debugging..

I would like to get back something like

API Call Made = "https://api.allthingstalk.io/login/parameters"..

Right now this is hard to debug..

    // This method gets the AccessToken from the API EndPoint, this token is encoded in a class Token with all its parameters..
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static Token getAccessToken () {

    Token accessToken = null;
    String urlParameters  = "param1=a&param2=b&param3=c";
    @SuppressLint({"NewApi", "LocalSuppress"}) byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );

    try {
        URL url = new URL("https://api.allthingstalk.io/login");
        HttpURLConnection client = null;

        client = (HttpURLConnection) url.openConnection();
        client.setRequestMethod("POST");
        client.setRequestProperty("grant_type", "password");
        client.setRequestProperty("username", username);
        client.setRequestProperty("password", password);
        client.setRequestProperty("client_id", "com.example.gebruiker.internetofthingsattplatform");
        client.setRequestProperty("Content-Type:", "application/x-www-form-urlencoded");
        client.setUseCaches(false);
        client.setDoOutput(true);

        int status = client.getResponseCode();
        String status_String = Integer.toString(status);
        Log.d(TAG, status_String);

        OutputStream outputStream = new BufferedOutputStream(client.getOutputStream());
        outputStream.write(postData);
        String postData_String = postData.toString();
        Log.d(TAG, postData_String);
        outputStream.flush();
        outputStream.close();


        if (client != null) // Make sure the connection is not null.
            client.disconnect();
    }
    catch(MalformedURLException error) {
    }
    catch(SocketTimeoutException error) {
    }
    catch (IOException error) {
    }

    return accessToken;
}

Upvotes: 0

Views: 589

Answers (1)

Dan 0
Dan 0

Reputation: 924

First, I'd highly recommend using a library like Retrofit as @Luis Henriques mentioned. Retrofit simplifies a lot of the complexities that go into building manual URLConnections.

That said, you're pretty much looking at your request exactly as it appears being sent. If you want a more granular view, I'd suggest changing your URL to a host you control (ie. localhost on an in home/office setup) and monitoring the connection via Wireshark (make sure you send it as a HTTP rather than HTTPS request though).

As a potential bug, you're passing your urlParameters as your post body. Wouldn't you want your URL parameters as part of the URL? Looking at the API documentation too: grant_type, password, username, and client_id, should be in your post body not set as a request property. setRequestProperty sets header properties.

Upvotes: 2

Related Questions