Recent

Author Topic: How use POST of unit fphttpclient  (Read 10186 times)

ezikoh

  • New Member
  • *
  • Posts: 32
How use POST of unit fphttpclient
« on: August 01, 2016, 08:36:09 pm »
Hello, I need send using POST HTTP with a Header where have one user and one key authorization, and send in the body a JSON data.

My question is, how use class TFPHTTPClient because a few examples founded I not understand or ther are incompleted.

Or where I found examples?

Thank you for your help

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How use POST of unit fphttpclient
« Reply #1 on: August 01, 2016, 09:57:35 pm »
My question is, how use class TFPHTTPClient because a few examples founded I not understand or ther are incompleted.
I like the hard way: read the source code http://svn.freepascal.org/svn/fpc/tags/release_3_0_0/packages/fcl-web/src/base/fphttpclient.pp
Or where I found examples?
http://svn.freepascal.org/svn/fpc/tags/release_3_0_0/packages/fcl-web/examples/httpclient/

I suggest creating the instance and assign properties values for complex request. Use AddHeader to add custom header and fill RequestBody with whatever format you want (basic streaming knowledge required).

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: How use POST of unit fphttpclient
« Reply #2 on: June 30, 2021, 08:08:40 am »
hello,
old post but here is a complete example of a post request on a testing https server . Since fpc 3.2 you can use the opensslsockets unit to manage https   ssl.
Code: Pascal  [Select][+][-]
  1. uses fphttpclient, opensslsockets;
  2.  
  3. procedure TForm1.Bt_PostClick(Sender: TObject);
  4. var
  5.  
  6.     Client: TFPHttpClient;
  7.     Response : TStringStream;
  8.     Params : string = '{"title": "titre","body": "corps","userId": 1}';
  9. begin
  10.     Client := TFPHttpClient.Create(nil);
  11.     Client.AddHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');
  12.     Client.AddHeader('Content-Type','application/json; charset=UTF-8');
  13.     Client.AddHeader('Accept', 'application/json');
  14.     Client.AllowRedirect := true;
  15.     client.RequestBody := TRawByteStringStream.Create(Params);
  16.     Response := TStringStream.Create('');
  17.     Memo1.Clear;
  18.     try
  19.         try
  20.             client.Post('https://jsonplaceholder.typicode.com/posts', Response);
  21.             Memo1.Append('Response Code of Post Request is ' +
  22.             inttostr(Client.ResponseStatusCode));
  23.             Memo1.Append('Réponse : ' + Response.DataString);
  24.         except on E:Exception do
  25.                 Memo1.Text := 'Something bad happened in Post Request : ' + E.Message;
  26.         end;
  27.     finally
  28.         Client.RequestBody.Free;
  29.         Client.Free;
  30.         Response.Free;
  31.     end;
  32.  
  33.  

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

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

 

TinyPortal © 2005-2018