How to parse Response from HttpURLConnection object in java - java

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"
}

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

Related

How to use HttpURLConnection to send request without handling response in Java

I have this code:
HttpURLConnection connection = (HttpURLConnection)(new URL(url)).openConnection();
connection.setRequestMethod("GET");
What I want is just to send a request to url.
I don't care about response.
I just need to send request.
How I do that?
You try can with post method instead of get method

how to get Body raw JSON data from Postman to Java

I have the variable customer-id and a value in the raw data in JSON format. I want to get the value from postman to java. How can I implement it?
It is not a good idea to use Postman in Java.
But you can do the same thing using Java.
First, you should call the same endpoint that Postman calls. You can use Unirest to access this endpoint.
Unirest.post("http://api.callme.com/json")
.asJson()
It is good to observe the METHOD that is called on Postman and use the same on Unirest. In the code above, I'm using the method POST.
Then, you will get a JSON object.
try this code
THis is a basic code to get the data from json data and please search
Download this jar file json
How to add jar file documentation here
HttpURLconnection
String url = "http//url"; //define the url here
URL obj; //making the object
obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
//add authorisation if you have any
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept-Language", "UTF-8");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
JSONObject jsonObject = new JSONObject(response.toString());
String nid = jsonObject.get("customer-id").toString();
You should have to set content type to application/json. And in your java web application side, you can fetch that json from request. If you are using servlet then you have to use request.getParameter("customer-id");

How to read Network Response (PAYLOAD) from GET API call using Java

I want to read the Network Response after API GET call using
URL obj = new URL("https://URLGoesHere");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
Refer attached image to understand what actually I want to read in Response
after API GET call.

HttpUrlConnection method is always GET on android

URL url = new URL("http://myserver.com/myendpoint");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//connection.setRequestMethod("POST") <- this didn't help either
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("string=test");
out.close();
connection.close()
The code above WORKS on desktop JVM, sends a post request, parsed on server-side successfully with response 200, however on android, the request method stays GET (yes I checked it IS false) and results in a 404 exception. Official docs say that setting doOutput to true triggers setting the request method to POST but that doesn't seem the case.
404 is not an exception. It is a HTTP status code returned by the server you make the request to and it means that the url you make the request to is not found. That has nothing to do with POST being set or not.
Things to check:
If the url you are making a request to is right.
If the server has a POST controller/handler mapped to the url you are making the request to.
Ask the guy who develops the server if he is handling the cases right ans if he's sending the correct response codes for the relevant scenarios.
Extra info: if the url is registered on the service but a POST request is not allowed you would get a 415 response code.
When posting data to a server, I'm setting some additional request header:
String query = "string=test";
URL url = new URL("http://myserver.com/myendpoint");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
connection.setFixedLengthStreamingMode(query.getBytes("UTF-8").length);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(query);
But as suggested, the 404 exception usually means, that the endpoint, you're trying to access, isn't available.
Try it:
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");

How to make REST API calls that have filter added

I am new to REST API and I want to make a REST API call which returns a JSON object
http://smlookup.dev/sec/products?search={"ABC.CP":"123A5"} - Runs fine in a browser and gives a JSON object
how do i get '?search={"ABC.CP":"12345"}' this expression to work as it filter the records
Code i am using is
String serverUrl="http://smlookup.dev/sec/products?search=";
String search=URLEncoder.encode("={\"ABC.CP\":\"12345\"}");
URL url = new URL(serverUrl+search);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("GET");
OutputStream out = httpCon.getOutputStream();
//FAILS GIVING 405 STATUS CODE
int responseCode = httpCon.getResponseCode();
All help or suggestions are helpful
Thanks!
Not sure if its normal but you dont send any data in your POST.
Furthermore you should urlencode your url, the inverted comma are not accepted like that.
URLEncoder.encode("={\"Xref.CP\":\"XAW3123A5\"}");

Categories