Yassir S
Yassir S

Reputation: 1042

Rest api call and POST request (java)

I am pretty new concerning REST api and POST request. I have the url of a REST api. I need to access to this api by doing an API call in JAVA thanks to a client id and a client secret (I found a way to hash the client secret). However, as I am new I don't know how to do that api call. I did my research during this all day on internet but I found no tutorial, website or anything else about how to do an api call. So please, does anyone know a tutorial or how to do that? (if you also have something about POST request it would be great)

I would be very thankful.

Thank you very much for your kind attention.

Sassir

Upvotes: 0

Views: 19551

Answers (3)

Thierry Templier
Thierry Templier

Reputation: 202286

The Restlet framework also allows you to do such thing thanks to its class ClientResource. In the code below, you build and send a JSON content within the POST request:

ClientResource cr = new ClientResource("http://...");

SONObject jo = new JSONObject();
jo.add("entryOne", "...");
jo.add("entryTow", "...");

cr.post(new JsonRepresentation(jo), MediaType.APPLICATION_JSON);

Restlet allows to send any kind of content (JSON, XML, YAML, ...) and can also manage the bean / representation conversion for you using its converter feature (creation of the representation based on a bean - this answer gives you more details: XML & JSON web api : automatic mapping from POJOs?).

You can also note that HTTP provides an header Authorization that allows to provide authentication hints for a request. Several technologies are supported here: basic, oauth, ... This link could help you at this level: https://templth.wordpress.com/2015/01/05/implementing-authentication-with-tokens-for-restful-applications/.

Using authentication (basic authentication for example) can be done like this:

String username = (...)
String password = (...)
cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password); 
(...)
cr.post(new JsonRepresentation(jo), MediaType.APPLICATION_JSON);

Hope it helps you, Thierry

Upvotes: 0

mats.nowak
mats.nowak

Reputation: 329

You can also use RestTemplate from Spring: https://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate

Fast and simple solution without any boilerplate code. Simple example:

RestTemplate rest = new RestTemplate();

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("firstParamater", "parameterValue");
map.add("secondParameter", "differentValue");

rest.postForObject("http://your-rest-api-url", map, String.class);

Upvotes: 1

James Watson
James Watson

Reputation: 464

Here's a basic example snippet using JDK classes only. This might help you understand HTTP-based RESTful services a little better than using a client helper. The order in which you call these methods is crucial. If you have issues, add a comments with your issue and I will help you through it.

URL target = new URL("http://www.google.com");
HttpURLConnectionconn = (HttpURLConnection) target.openConnection();
conn.setRequestMethod("GET");

// used for POST and PUT, usually
// conn.setDoOutput(true);
// OutputStream toWriteTo = conn.getOutputStream();

conn.connect();
int responseCode = conn.getResponseCode();

try 
{
    InputStream response = conn.getInputStream();
}
catch (IOException e)
{
    InputStream error = conn.getErrorStream();
}

Upvotes: 1

Related Questions