How to parse Response from HttpURLConnection object in java

How to parse Response from HttpURLConnection object in java

Problem Description:

I am trying to validate OTP via 2Factor API but don’t know how to parse response data in java code.
tested on Postman `

URL url = new URL("https://2factor.in/API/V1/{api_key}/SMS/VERIFY/{session_id}/{otp_entered_by_user}");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

tested the above API in postman and getting response like this,

{
    "Status": "Success",
    "Details": "OTP Matched"
}

Solution – 1

You can get a reference to the input stream from the connection and read the contents. See the link below for an example where it explains both GET and POST requests

https://www.digitalocean.com/community/tutorials/java-httpurlconnection-example-java-http-request-get-post

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject