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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| var
RESTClient: TRESTClient;
RESTRequest: TRESTRequest;
RESTResponse: TRESTResponse;
Authentification: THTTPBasicAuthenticator;
ImageStream: TMemoryStream;
FileName: string;
begin
// Initialisation des composants REST
RESTClient := TRESTClient.Create(nil);
RESTRequest := TRESTRequest.Create(nil);
RESTResponse := TRESTResponse.Create(nil);
Authentification := THTTPBasicAuthenticator.Create(nil);
try
RESTClient.BaseURL := 'https://votrenomdedomain/wp-json/wp/v2/media';
Authentification.Username := 'user';
Authentification.Password := 'password';
RESTRequest.Resource:='wp/v2/media';
RESTClient.ContentType := 'application/json';
RESTClient.Authenticator := Authentification;
// Configuration de la requête REST
RESTRequest.Client := RESTClient;
RESTRequest.Response := RESTResponse;
RESTRequest.Method := rmPOST;
//******************************************************************
ImageStream := TMemoryStream.Create;
ImageStream.LoadFromFile(FileName);
ImageStream.Position := 0;
RESTRequest.AddBody(ImageStream , TRESTContentType.ctIMAGE_JPEG );
//******************************
RESTRequest.Execute;
//*******************************
// Vérification de la réponse
if RESTResponse.StatusCode = 200 then
ShowMessage('L''image a été envoyée avec succès.')
else
ShowMessage('Erreur lors de l''envoi de l''image : ' + RESTResponse.Content);
Edit1.Text:= RESTResponse.Content;
finally
// Libération des ressources
RESTClient.Free;
RESTRequest.Free;
RESTResponse.Free;
Authentification.Free;
end;
end; |
Partager