Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Web Service REST

Client
Plan

En Java
Prérequis
Exemples
Etapes
En PHP
Prérequis
Exemples
Etapes

2
En Java - Prérequis

Plusieurs possibilités pour écrire un client REST,


entre autres :
●Java SE
Ne nécessite pas de librairie, mais requiert complexe à
utiliser
●Apache HttpClient :
Plus simple que JSE, mais n’est pas assez standardisé
●Jersey Client :
Plus simple que JSE et utilise plus de standard; C’est ce que
nous allons utiliser

3
En Java - Prérequis

Jersey Client
Pour utiliser Jersey Client; il faudrait ajouter sa librairie au
projet.

4
En Java - Exemples

Requête POST avec des paramètres


Client client = ClientBuilder.newClient();
WebTarget target =
client.target("http://localhost:9998").path("resource");

Form form = new Form();


form.param("x", "foo");
form.param("y", "bar");

MyBean bean =
target.request(MediaType.APPLICATION_XML_TYPE)

.post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
MyBean.class);

5
En Java - Exemples

Exemple détaillé
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MyClientResponseFilter.class);
clientConfig.register(new AnotherClientFilter());

Client client = ClientBuilder.newClient(clientConfig);


client.register(ThirdClientFilter.class);

WebTarget webTarget = client.target("http://example.com/rest");


webTarget.register(FilterForExampleCom.class);
WebTarget resourceWebTarget = webTarget.path("resource");
WebTarget helloworldWebTarget = resourceWebTarget.path("helloworld");
WebTarget helloworldWebTargetWithQueryParam =
helloworldWebTarget.queryParam("greeting", "Hi World!");

Invocation.Builder invocationBuilder =
helloworldWebTargetWithQueryParam.request(MediaType.TEXT_PLAIN_TYPE);
invocationBuilder.header("some-header", "true");

Response response = invocationBuilder.get();


System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));

6
En Java - Exemples

Exemple compact
Client client = ClientBuilder.newClient(new ClientConfig()
.register(MyClientResponseFilter.class)
.register(new AnotherClientFilter()));

String entity = client.target("http://example.com/rest")


.register(FilterForExampleCom.class)
.path("resource/helloworld")
.queryParam("greeting", "Hi World!")
.request(MediaType.TEXT_PLAIN_TYPE)
.header("some-header", "true")
.get(String.class);

7
En Java - Etapes

1) Créer un client HTTP, éventuellement configuration


Client client = ClientBuilder.newClient();
Client client = ClientBuilder.newClient(ClientConfig);

2) Cibler la ressource
WebTarget webTarget = client.target("http://example.com/rest");

3) Identifier la ressource
WebTarget resourceWebTarget = webTarget.path("resource");

4) Invocation de la requête HTTP et traitement


Invocation.Builder invocationBuilder =
helloworldWebTargetWithQueryParam.request(MediaType.TEXT_PLAIN_TYPE);
invocationBuilder.header("some-header", "true");
Response response = invocationBuilder.get();
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));

8
Plan

En Java
Prérequis
Exemples
Etapes
En PHP
Prérequis
Exemples
Etapes

9
En PHP - Prérequis

Il faut s’assurer que l’extension CURL de PHP est


activé

10
En PHP - Exemples

Exemple GET
$service_url =
'http://example.com/api/conversations/125/messages&apikey=145';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' .
var_export($info));
}
curl_close($curl);
echo $curl_response)
echo 'response ok!';

11
En PHP - Exemples

Exemple POST
$service_url = 'http://example.com/api/conversations';
$curl = curl_init($service_url);
$curl_post_data = array(
'message' => 'test message',
'useridentifier' => 'agent@example.com',
'department' => 'departmentId001',
'subject' => 'My first conversation',
'recipient' => 'recipient@example.com',
'apikey' => 'key001'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
echo $curl_response;

12
En PHP - Exemples

Exemple PUT
$service_url = 'http://example.com/api/conversations/cid123/status';
$ch = curl_init($service_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$data = array("status" => 'R');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
if ($response === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($ch);
echo $response;

13
En PHP - Exemples

Exemple DELETE
$service_url = 'http://example.com/api/conversations/25';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$curl_post_data = array(
'note' => 'this is spam!',
'useridentifier' => 'agent@example.com',
'apikey' => 'key001'
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$response = curl_exec($ch);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
echo $curl_response;

14
En PHP - Etapes

1) Initialisation de la connexion
$curl = curl_init($service_url);

2) Configuration des options


curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
// …voir la documentation de curl_setopt()

3) Exécuter la requête et exploiter le résultat


$curl_response = curl_exec($curl);

15
Références

Protocole HTTP
https://fr.wikipedia.org/wiki/Hypertext_Transfer_Protocol
Liste des codes de réponse HTTP
https://fr.wikipedia.org/wiki/Liste_des_codes_HTTP

16

You might also like