RESTClient, RESTRequest, RESTResponse
Bonjour,
je tourne en rond en utilisant ces composants pour discuter avec une API Rest.
Les solutions que j'ai trouvées pour ajouter des valeurs dans le Header du message POST
RESTClient1.Params.AddItem('Authorization', 'Bearer ' + GBL_AccessToken, TRESTRequestParameterKind.pkHTTPHEADER);
ou
RESTClient1.SetHTTPHeader('Authorization', 'Bearer ' + GBL_AccessToken);
ne marchent à priori pas. La première ajoute la valeur dans le Body, la seconde ne fait rien.
Par ailleurs je n'ai pas trouvé non plus le moyen de voir ni la liste des valeurs du Header, ni la chaine complète envoyée en JSON à l'URL.
des Idées ou vécu, car je n'ai pas su trouver d'exemple qui marche ?
J'utilise Delphi 12
Merci par avance
j'ai essayé les 3 méthodes que j'ai trouvé
J'ai un peu avancé grace au lien de Sergio,
mais je reste bloquée sur un header indispensable.
mon 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| function TFormNetwork.RequestAPI(Chaine : string): string;
var
MyRESTClient: TRESTClient;
MyRESTRequest: TRESTRequest;
MyRESTResponse: TRESTResponse;
MyOAuth2Authenticator: TOAuth2Authenticator;
begin
Result := '';
MyRESTClient := TRESTClient.Create(WC_BASE_URL);
MyRESTRequest := TRESTRequest.Create(Self);
MyRESTResponse := TRESTResponse.Create(Self);
MyOAuth2Authenticator:= TOAuth2Authenticator.Create(Self);
try
formLogs.LogSystem('Request to Silae''s API: ' + Chaine);
MyRESTRequest.Client := MyRESTClient;
MyRESTRequest.Response := MyRESTResponse;
MyRESTClient.BaseURL := WC_BASE_ENDPOINT + Chaine;
MyRESTClient.Authenticator := MyOAuth2Authenticator;
MyRESTClient.ContentType := 'application/json';
MyOAuth2Authenticator.ClientID := CLIENT_ID;
MyOAuth2Authenticator.ClientSecret := CLIENTSECRET_PRIMAIRE;
MyOAuth2Authenticator.AccessToken := GBL_AccessToken;
MyOAuth2Authenticator.TokenType := TOAuth2TokenType.ttBEARER;
MyOAuth2Authenticator.ResponseType := TOAuth2ResponseType.rtCODE;
MyOAuth2Authenticator.Scope := scope;
// MyRESTClient.Params.AddHeader('Authorization', 'Bearer ' + GBL_AccessToken);
MyRESTClient.Params.AddHeader('Ocp-Apim-Subscription-Key', CLE_PRINCIPALE);
MyRESTClient.Params.AddHeader('dossiers;', '');
MyRESTClient.Params.AddItem('dossiers;', '', TRESTRequestParameterKind.pkHTTPHEADER);
MyRESTClient.SetHTTPHeader('dossiers;', '');
//
MyRESTClient.Params.AddItem('typeDossiers', '1');
//
try
MyRESTRequest.Method := rmPOST;
MyRESTRequest.Execute;
formMain.MemoRequest.Lines.Add(GetRESTClientInfoAsString(MyRESTClient, MyRESTRequest)); //GetRESTClientInfoAsString(RESTClient1, RESTRequest1);
formLogs.LogSystem('');
Result := MyRESTResponse.JSONText;
except
on E: Exception do formLogs.LogExcept('Except on RequestAPI: ' + E.Message);
end;
finally
FreeAndNil(MyRESTClient);
FreeAndNil(MyRESTRequest);
FreeAndNil(MyRESTResponse);
FreeAndNil(MyOAuth2Authenticator);
end;
end; |
Cela me retourne :
{
"errors": [
{
"code": "403",
"message": "Le header 'dossiers' est obligatoire"
}
],
"source": "Proxy"
}