0

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

Yassir S
  • 1,032
  • 3
  • 21
  • 44

3 Answers3

1

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();
}
James Watson
  • 464
  • 2
  • 11
  • Thank you. I modified the code to be suitable for https like in my case. But, I don't know how to use my client id and client signature headers. Do you know how to do ? Then, to connect to an API account using an email and a password, I have to make a POST request to https://url/../ with body arguments email and password (I don't know either) – Yassir S Feb 13 '15 at 21:22
1

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);
mats.nowak
  • 329
  • 1
  • 7
0

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

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360