Connection API REST OK / JSON
:lol:
Je reviens sur le forum pour poster mon code qui fonctionne;
J'utilise la librairie indy 10. Attention, il ne faut pas oublier de copier sslEay32.dll et libeay32.dll dans le dossier du projet.
voici le code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
function Tform1.Connecter(user,mdp,id:string):string;
var
HTTP: TIdHTTP;
RequestBody: TStringStream;
ResponseBody: string;
jsonData:TJsonData;
begin
try
HTTP := TIdHTTP.Create;
// init du request body avec les paramètres autorisation spécifiés dans l'API REST utilisée
RequestBody := TStringStream.Create('{"user": "'+user+'","password": "'+mdp+'","id": "'+id+'"}');
try
HTTP.Request.Accept := 'application/json';
HTTP.Request.ContentType := 'application/json';
// URL de connection à l'API REST
ResponseBody := HTTP.Post('https://www.xxx',RequestBody);
// ResponseBody est sous forme JSON ==> spécification de l'API REST utilisée
jsonData:=GETJSON(ResponseBody); // ===> nécessaire ?
// dans l'objet JSON, je récupère la reponse de la connexion, ici le nom JSON est 'retour' (spécification de l'API REST utilisée)
showMessage(jsonData.FindPath('retour').asString);
// la fonction retourne la valeur du jeton dautorisation du nom de l'objet JSON (spécification de l'API REST utilisée)
Connecter:=jsonData.FindPath('data.jeton').AsString;
finally
RequestBody.Free;
end;
HTTP.Free;
except
on E: EIdHTTPProtocolException do
begin
WriteLn(E.Message);
WriteLn(E.ErrorMessage);
end;
on E: Exception do
begin
WriteLn(E.Message);
end;
end;
end; |
voilà, j'aimerai avoir votre avis.
Petite question : quelle est la différence entre GETJSON et JSON.parse ?
A+
API REST utilisation bibliotheque free pascal
Bonjour
Bon c'est OK, j'utilise la bibliothèque free pascal. Tout Fonctionne.
merci