Home » Java » java – Okhttp3 Base URL for calling-Exceptionshub

java – Okhttp3 Base URL for calling-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am using an OkHttp Client for my Webservice call, its working successfully with JAVA Main function and returning response.

Now, I want to call this client from Postman; but I dont know what URL Shall i call? Like in jersey we create a base URL from @PATH annotation in Spring we use @service. What shall be done in okhttp to create a base url to call? Please help.

    public String soapCaller() throws IOException, JSONException  {

        Response response = null;
try {
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=0146b9a4-7e99-4c83-8e9e-6049cfec55da&client_secret=cJ5nD0yJ4fV8eM1nU4tK2yI5wQ0lG6iE7cP5bD4lQ8dB0jS6pV&scope=ABLApis");
Request request = new Request.Builder()
  .url("https://uat-api.abl.com/abl-api/uat/oauth2/token")
  .post(body)
  .addHeader("content-type", "application/x-www-form-urlencoded")
  .addHeader("accept", "application/json")
  .build();
 response = client.newCall(request).execute();
}

catch(Exception e)
{
    logger.info("e: " + e);
}
return response.toString();
    }
    }

with Jersey Annotations

    @GET
    @Path("/fundTransfer")
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
    public String soapCaller() throws IOException, JSONException  {

        /////////////////////////////



        Response response = null;
try {
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=0146b9a4-7e99-4c83-8e9e-6049cfec55da&client_secret=cJ5nD0yJ4fV8eM1nU4tK2yI5wQ0lG6iE7cP5bD4lQ8dB0jS6pV&scope=ABLApis");
Request request = new Request.Builder()
  .url("https://uat-api.abl.com/abl-api/uat/oauth2/token")
  .post(body)
  .addHeader("content-type", "application/x-www-form-urlencoded")
  .addHeader("accept", "application/json")
  .build();
 response = client.newCall(request).execute();
}

catch(Exception e)
{
    logger.info("e: " + e);
}




///////////////////////////////////////////



return response.toString();
    }
    }
How to&Answers: