How to use htts Get Request with Headers lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- xamsin · 2019-09-08 07:57 I'm newbie in Lazarus, need to wright a client where I need to use the Get Request to HTTPS Service with Headers my test procedure Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Classes, SysUtils, fphttpclient, Forms,  Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, PairSplitter, fpopenssl;    procedure TForm2.Button1Click(Sender: TObject); var   QueryParams:TStrings=nil;   AURL:string;   s:string='';   item:string;   l: TStringstream;   http: tfphttpclient;begin l := TStringStream.Create(''); http:=tfphttpclient.Create(nil); With http do    try      QueryParams:=TStringList.Create;      AddHeader('Authorization', 'AccessToken ///');      AddHeader('X-User- Authorization', 'Basic ///');      AddHeader('Content- Type','application/json;charset=UTF-8');      with QueryParams do     begin        if not String(lbedSettlement.Text).IsEmpty then         Values['settlement']:=lbedSettlement.Text;        if not String(lbedRegion.Text).IsEmpty then           Values['region']:=lbedRegion.Text;        if not String(lbedDistinct.Text).IsEmpty then           Values['distict']:=lbedDistinct.Text;       end;      AURL:='https://otpravka- api.pochta.ru/postoffice/1.0/settlement.offices.codes';      For item in QueryParams do          s:=s+'&'+ item;      AURL:=AURL+'?' + s.Substring(1);      httpmethod('GET',AURL,l,[]);      memo1.Lines.Clear;      memo1.Lines.Append(inttostr(ResponseStatusCode) + ' ' + ResponseStatusText);      memo1.Lines.Append(ResponseHeaders.Text);    memo1.Lines.Append(l.DataString);    finally      Free;      QueryParams.Free;      l.Free;    end; end;  I've also used synapse library but I always get the 400 bad request. can anybody helps me where i wrong? ---------------------------------------------------------------------- Thaddy · 2019-09-08 09:57 Add a user-agent, eg.:AddHeader('User-Agent', 'Mozilla/5.0 (compatible myclient)'); Your basic code should look like this ( removed unnecessary processing): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi}{$H+}uses classes, fphttpclient,fpopenssl, opensslsockets; var   AURL:string;   l: TStringstream;   http: tfphttpclient;begin l := TStringStream.Create(''); http:=tfphttpclient.Create(nil); With http do    try      AllowRedirect := true; // because it uses a redirect, otherwise 301 moved permanently      QueryParams:=TStringList.Create;      AddHeader('User-Agent','Mozilla 5.0 (compatible ct)'); // because it needs a modern browser string      AddHeader('Authorization', 'AccessToken MjtAFOrgYUrsfCC7KPLpAi03N4Od17Bh');      AddHeader('X-User-Authorization', 'Basic aW5mb0BzcG1hc2gucnU6NTE0NzU4');      AddHeader('Content- Type','application/json;charset=UTF-8');      AURL:='https://otpravka- api.pochta.ru'; // add the rest again in  your own code      httpmethod('GET',AURL,l,[]);      writeln(ResponseStatusCode , ' ',ResponseStatusText);      writeln(ResponseHeaders.Text);    finally      Free;    end;end. ---------------------------------------------------------------------- xamsin · 2019-09-08 11:55 I added AddHeader('User-Agent', 'Mozilla/5.0 (compatible myclient)'); but I also '400 Bad Request'. what's wrong ---------------------------------------------------------------------- Thaddy · 2019-09-08 11:59 Post crossed, see the example I added above. that is verified to work!. In your own code just add the first two lines that I embellished with a comment: - AllowRedirect - User-Agent. With my working example code you will obtain:Code: Bash  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 200 OKServer: nginxDate: Sun, 08 Sep 2019 12:04:21 GMTContent-Type: text/html;charset=utf-8Transfer-Encoding: chunkedConnection: closeContent-Language: ruStrict-Transport-Security: max-age=31536000; includeSubDomainsAll that means you can set up a valid connection with your user credentials!! The rest is up to you... And that will also work with your code....because the rest of your code is fine. P.s.: you published your key!, don't do that! We are smart enough to obtain our own key for testing......< > Really, never do that. Now I have to ask the moderators to remove the conversation or edit it to remove your key when you can confirm you got it working and if I were you I'd ask for a new key. ---------------------------------------------------------------------- xamsin · 2019-09-08 12:17 it is the test key open key ---------------------------------------------------------------------- Thaddy · 2019-09-08 12:21 Ok. Does it work now? ---------------------------------------------------------------------- xamsin · 2019-09-08 14:45 not this key now doesn't work. but i was found whats wrong the string must be in that format %27%D0%92%D0%BB%D0%B0%D0%B4%D0%B8%D0%BA%D0%B0%D0%B2%D0%BA%D0%B0%D0%B7% 27 how I can do this. Utf8Encode doesn't take me this format ---------------------------------------------------------------------- lucamar · 2019-09-08 15:13 I think there is a ready-made function for that (probably in fcl-xml) but this should work too: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function PercentEncode(const AString: String): String;var  i: Integer;begin  Result := '';  if AString <> '' then    for i := 1 to Length(AString) do      Result := Result + '%' + IntToHex(Ord(AString[i]), 2);end; ---------------------------------------------------------------------- Thaddy · 2019-09-08 16:24 Sigh, that does the code for you. See my valid connection. ---------------------------------------------------------------------- xamsin · 2019-09-08 16:50 thanks much for you all I Used EncodeURLElement function but why it doesn't want to work with out this translation i don't understand.