Recent

Author Topic: Tfphttpclient adding post varibles  (Read 2354 times)

cpalx

  • Hero Member
  • *****
  • Posts: 753
Tfphttpclient adding post varibles
« on: August 19, 2021, 07:11:47 pm »
Hello,

using
Code: Pascal  [Select][+][-]
  1. HttpClient.HTTPMethod('POST', URL, SS, []);
  2.  

the way add POST varibles is like this?


Code: Pascal  [Select][+][-]
  1. Params:= TStringList.Create;
  2.     Params.Values[ 'Info' ]:= '{ "tagid" : "2", "Params" : { "p0" : -1 } }';
  3.     Params.Values[ 'Header' ]:= '{ "AddDefaultParams": false, "workid": 1 }';
  4.     HttpClient.RequestBody:= TRawByteStringStream.Create(Params.Text);
  5.     Params.Free;
  6.     HttpClient.HTTPMethod('POST', URL, SS, []);
  7.  

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Tfphttpclient adding post varibles
« Reply #1 on: August 20, 2021, 01:37:03 am »
hello,
here is an example to post json request :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button4Click(Sender: TObject);
  2. var
  3.  
  4.     Client: TFPHttpClient;
  5.     Response : TStringStream;
  6.     Params : string = '{"title": "titre","body": "corps","userId": 1}';
  7. begin
  8.     Client := TFPHttpClient.Create(nil);
  9.     Client.AddHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');
  10.     Client.AddHeader('Content-Type','application/json; charset=UTF-8');
  11.     Client.AddHeader('Accept', 'application/json');
  12.     Client.AllowRedirect := true;
  13.     client.RequestBody := TRawByteStringStream.Create(Params);
  14.     Response := TStringStream.Create('');
  15.     Memo1.Clear;
  16.     try
  17.         try
  18.             client.Post('https://jsonplaceholder.typicode.com/posts', Response);
  19.             Memo1.Append('Response Code of Post Request is ' +
  20.             inttostr(Client.ResponseStatusCode));
  21.             Memo1.Append('Réponse : ' + Response.DataString);
  22.         except on E:Exception do
  23.                 Memo1.Text := 'Something bad happened in Post Request : ' + E.Message;
  24.         end;
  25.     finally
  26.         Client.RequestBody.Free;
  27.         Client.Free;
  28.         Response.Free;
  29.     end;
  30. end;      

Result :
Quote
Response Code of Post Request is 201
Réponse : {
  "title": "titre",
  "body": "corps",
  "userId": 1,
  "id": 101
}

https://jsonplaceholder.typicode.com  is a free server to test http requests.

Friendly, J.P

Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

cpalx

  • Hero Member
  • *****
  • Posts: 753
Re: Tfphttpclient adding post varibles
« Reply #2 on: August 20, 2021, 02:43:16 am »
Thanks

 

TinyPortal © 2005-2018