msd 5 Posted March 25, 2021 Hello developers, I need small expert assistance for translating old TIdHTTP requests to new TNetHTTPRequest. I have one function for Get and one function for Post, but credentials in the header do not have the same logic, and if someone can translate it to a new TNetHTTPRequest way of web service connection? function appGetData(API): boolean; var Url: string; lHTTP: TIdHTTP; xml: IXMLDocument; XmlRS, HttpRS, HttpRC: string; begin HttpRC := ''; HttpRS := ''; if Port = '' then Url := dm.apiHttp + '://' + dm.Server + '/api' + API else Url := dm.apiHttp + '://' + dm.Server + ':' + dm.Port + '/api' + API; try lHTTP := TIdHTTP.Create; try lHTTP.Request.Clear; lHTTP.Request.BasicAuthentication := true; lHTTP.Request.Accept := 'application/xml'; lHTTP.Request.UserName := dm.UserName; lHTTP.Request.Password := dm.Password; lHTTP.Request.ContentEncoding := 'utf-8'; lHTTP.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)'; lHTTP.Request.CustomHeaders.AddValue('OrganisationId', dm.Company); lHTTP.Request.CustomHeaders.AddValue('Accept', 'application/xml'); lHTTP.HTTPOptions := [hoForceEncodeParams]; XmlRS := lHTTP.Get(Url); HttpRC := IntToStr(lHTTP.ResponseCode); HttpRS := lHTTP.ResponseText; except end; lHTTP.Free; if HttpRC = '200' then Result := true else Result := false; finally end; end; function PostData(API, xml: string): boolean; var Url: string; lHTTP: TIdHTTP; XmlData: TStringStream; XmlRS, HttpRS, HttpRC: string; begin HttpRC := ''; HttpRS := ''; XmlData := TStringStream.Create(xml); if Port = '' then Url := dm.apiHttp + '://' + Server + '/api' + API else Url := dm.apiHttp + '://' + Server + ':' + Port + '/api' + API; try lHTTP := TIdHTTP.Create; try lHTTP.Request.Clear; lHTTP.Request.BasicAuthentication := true; lHTTP.Request.Accept := 'application/xml'; lHTTP.Request.ContentType := 'application/xml'; lHTTP.Request.UserName := dm.UserName; lHTTP.Request.Password := dm.Password; lHTTP.Request.ContentEncoding := 'utf-8'; lHTTP.Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)'; lHTTP.Request.CustomHeaders.AddValue('OrganisationId', dm.Company); lHTTP.Request.CustomHeaders.AddValue('Accept', 'application/xml'); lHTTP.HTTPOptions := [hoForceEncodeParams]; XmlRS := lHTTP.Post(Url, XmlData); HttpRC := IntToStr(lHTTP.ResponseCode); HttpRS := lHTTP.ResponseText; except end; XmlData.Free; lHTTP.Free; if HttpRC = '200' then Result := true else Result := false; finally end; end; Thanks for any help in the advance :-) Share this post Link to post
Remy Lebeau 1393 Posted March 25, 2021 2 hours ago, msd said: but credentials in the header do not have the same logic In what way exactly? Rather than someone translating your TIdHTTP code, can you show your existing TNetHTTPRequest code that is not working for you? Share this post Link to post
msd 5 Posted March 25, 2021 Hello Remy, I need some code rewritten to use HTTPClient & HTTPRequest instead of IdHTTP. Just someone need to translate my code (my functions) if he has some minutes free 🙂 Share this post Link to post
amit 3 Posted April 2, 2021 Just try and let me know if it works. uses ..., System.NetEncoding, ... ... var Base64: TBase64Encoding; httpClient: THttpClient; ... begin ... httpClient.ContentType:='application/xml'; Base64 := TBase64Encoding.Create; httpClient.CustHeaders.add('Authentication','Basic ' + Base64.Encode('user:password')); Base64.Free; httpClient.CustHeaders.Add('OrganisationId', 'your dm.Company'); httpClient.CustHeaders.Add('Accept', 'application/xml'); httpClient.Post('your URL',xmlData); ... end; Share this post Link to post
msd 5 Posted April 2, 2021 Hello Amit, It works fine, thank you for a assistance :-) Share this post Link to post
Remy Lebeau 1393 Posted April 2, 2021 (edited) 15 hours ago, amit said: httpClient.CustHeaders.add('Authentication','Basic ' + Base64.Encode('user:password')); The preferred way to specify authentication credentials in THttpClient is to use its CredentialStorage property, eg: httpClient.CredentialStorage.AddCredential(TCredential.Create(TAuthTargetType.Server, '', '', 'username', 'password')); Or its AuthEvent event: procedure TMyForm.HTTPClientAuthEvent(const Sender: TObject; AnAuthTarget: TAuthTargetType; const ARealm, AURL: string; var AUserName, APassword: string; var AbortAuth: Boolean; var Persistence: TAuthPersistenceType); begin if AnAuthTarget = TAuthTargetType.Server then begin AUserName := 'username'; APassword := 'password'; end; end; httpClient.AuthEvent := HTTPClientAuthEvent; See Using an HTTP Client: Handling Authentication and Certificates in Embarcadero's DocWiki. Edited April 2, 2021 by Remy Lebeau 1 Share this post Link to post
amit 3 Posted April 3, 2021 (edited) Dear Remy, Are these CredentialStorage and AuthEvent limited to only Basic Authentication? Is there any processing different among using CustHeaders and the methods you suggested? And many thanks for your suggestion. Edited April 3, 2021 by amit Share this post Link to post
Remy Lebeau 1393 Posted April 3, 2021 1 minute ago, amit said: Are these CredentialStorage and AuthEvent limited to only Basic Authentication? I have no idea. I don't use T(Net)HttpClient, and don't even have the RTL source code for them to look at. Share this post Link to post