how to get Body raw JSON data from Postman to Java - 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");

Related

How to parse Response from HttpURLConnection object in 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

Java make HTTP call to AWS

I have set up in AWS a private API gateway, I can call it use curl like this to access it:
curl -v https://vpce-02e21f9af4f10cf2a-6y6qzepe-ap-southeast-2b.execute-api.ap-southeast-2.vpce.amazonaws.com/Test/uppercase/zzzddd -H'Host:515btqa443.execute-api.ap-southeast-2.amazonaws.com'
I can also use Postman by set the URL and set the header.
However, I seem to have an issue accessing it from Java, I think it is not adding the Header and I get 403 forbidden message.
String urlstr = "https://vpce-02e21f9af4f10cf2a-6y6qzepe-ap-
southeast-2b.execute-api.ap-southeast-
2.vpce.amazonaws.com/Test/uppercase/zzzddd";
URL url = new URL(urlstr);
HttpURLConnection con = (HttpURLConnection)
url.openConnection();
con.setRequestMethod("GET");
con.addRequestProperty("Host", "515btqa443.execute-api.ap-southeast-2.amazonaws.com");
int responseCode = con.getResponseCode();
Any ideas if i am doing something wrong in the Java ?

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.

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\"}");

android HTTP POST fail

I'm trying to connect and post to a simple java webservice, running the post's URL from chrome succeeded, but android code skip the following lines (without throwing errors), but the webservice doesn't accept the post
HttpPost post = new HttpPost(setFacebookEventsAddress+userId+"/"+accesstoken);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(post);
the webservice method signature handling the above request:
#GET
#Path("setData/{user_id}/{accessToken}")
#Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8")
public String setData(#PathParam("user_id") String user_id,
#PathParam("accessToken") String accessToken) {
since I manage to post throw my browser, anyone can help with what's wrong with my android code?
URL url = new URL(setFacebookEventsAddress+userId+"/"+accesstoken);
HttpURLConnection con = (HttpURLConnection) url
.openConnection();
ja = readStream(con.getInputStream());
Using HttpURLConnection instead of HttpPost did the trick for me, thanks for all the helpers!
It is not possible to say with any certainty (given the evidence), but my guess would be that the expression
setFacebookEventsAddress + userId + "/" + accesstoken
is evaluating to a different URL to the one you are using from the web browser.
I suggest that you try the following:
Turn on request logging on your server, and compare the URLs in the requests being sent.
Modify your client to print out the response status code and the response body. The latter is likely to be an error page that will give you more clues.
Another possible problem is that your code doesn't appear to be sending any body with the POST request.
On revisiting this, the problem was that you were using / trying to do a POST to a web service that you had configured to support GET only. I expect that if you had looked at the status code you would have found that the response code was "Method not supported".

Categories