0
votes

I'm trying to Post a JSON to a Rest service using RestAssured and it throws java.net.ConnectException error.
Since my JSON message is complex, I've made it available within the txt file.

public class PPJson {

    public static void main(String[] args) throws IOException, JSONException {

        BufferedReader tempreader = new BufferedReader(new FileReader("/Users/RestService/Jsonfileinput.txt"));
        StringBuilder sbuilder = new StringBuilder();
        String line = tempreader.readLine();

            while (line != null) {
                sbuilder.append(line);
                line = tempreader.readLine();
            }
            tempreader.close();

        //Initializing Rest API's URL 
        String APIUrl = "http://api.sample.com/api/v3/parcel/collect?apiKey={Value}"; 

        //Initializing payload or API body 
        String APIBody = sbuilder.toString();

        // Building request using requestSpecBuilder 
        RequestSpecBuilder builder = new RequestSpecBuilder().setBody(APIBody).setContentType("application/json; charset=UTF-8"); 

        RequestSpecification requestSpec = builder.build(); 

        //Making post request with authentication 
        Response response = given().authentication().preemptive().basic("","").spec(requestSpec).when().post(APIUrl);

        JSONObject JSONResponseBody = new JSONObject(response.body().asString()); 

        //Fetching the desired value of a parameter 
        String result = JSONResponseBody.getString("status");

        System.out.println(result);

        }

    }
1
Have you double-checked that your APIUrl is correct and that you're attempting to connect to the right port? From the docs docs.oracle.com/javase/7/docs/api/java/net/…: ConnectException: Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port). - spg
Simon, I was able to post and get a response from POSTMAN for the same rest URL. Do we have to include anything in addition to get the same response by executing this Java code? - Sathish Rkrish
Can you post the response and request (url, body, headers) that worked in Postman? - spg
@Simon-Pierre, I have given the details as a post below. And also, I'm using Eclipse IDE under Mac to run this program. - Sathish Rkrish
@Simon-Pierre, any luck with the resolution for this issue? - Sathish Rkrish

1 Answers

0
votes

@all, the issue has been fixed, removed the references to RequestSpecs and here's the code snippet

public class ParcelPointJson {

    public static void main(String[] args) throws IOException, JSONException {

        BufferedReader tempreader = new BufferedReader(new FileReader("/Users/srkrish/gitTrial/pim-automation/RestService/Jsonfileinput.txt"));
        StringBuilder sbuilder = new StringBuilder();
        String line = tempreader.readLine();

            while (line != null) {
                sbuilder.append(line);
                line = tempreader.readLine();
            }
            tempreader.close();

        //Initializing Rest API's URL 
        String APIUrl = "http://api.staging.wow.parcelpoint.com.au/api/v3/parcel/collect?apiKey=09BH1TXV";

        //Initializing payload or API body 
        String APIBody = sbuilder.toString();

        //Making post request with authentication 
        Response response = given().contentType("application/json; charset=UTF-8").body(APIBody).when().post(APIUrl);

        JSONObject JSONResponseBody = new JSONObject(response.body().asString()); 

        //Fetching the desired value of a parameter 
        String result = JSONResponseBody.getString("parcelId");

        System.out.println(result);

        //Assert.assertEquals(result, "200"); 

        }

    }