Recent

Author Topic: Get and post request to URL  (Read 35499 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Get and post request to URL
« on: January 24, 2018, 06:48:30 pm »
I need interact with a rest api server. I found it is easy to send get request with fphttpclient:

Code: Pascal  [Select][+][-]
  1. function ReadURLGet(url:string):string;
  2. begin
  3. with TFPHTTPClient.Create(nil) do
  4. try
  5.   ReadURL:= Get(url);
  6. finally
  7.   Free;
  8. end;
  9.  
  10. end;  

Since server answer is always a Json format string, it works perfect for me. But how about post request? Is there any simple way with fphttpclient? Something like:

Code: Pascal  [Select][+][-]
  1. function ReadURLPost(url:string;data:array):string;
  2. begin
  3. with TFPHTTPClient.Create(nil) do
  4. try
  5.   ReadURL:= Post(url,data);
  6. finally
  7.   Free;
  8. end;
  9.  
  10. end;

Any simple code for "example.com", using user:jhondoe and pass:9999 as post data?

« Last Edit: January 25, 2018, 02:49:43 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

balazsszekely

  • Guest
Re: Get and post request to URL
« Reply #1 on: January 24, 2018, 06:53:26 pm »
The easiest way is:
Code: Pascal  [Select][+][-]
  1.   with TFPHTTPClient.Create(nil) do
  2.   try
  3.      SimplePost(); // or Post();
  4.   finally
  5.     Free;
  6.   end;

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Get and post request to URL
« Reply #2 on: January 24, 2018, 06:55:03 pm »
The easiest way is:
Code: Pascal  [Select][+][-]
  1.   with TFPHTTPClient.Create(nil) do
  2.   try
  3.      SimplePost(); // or Post();
  4.   finally
  5.     Free;
  6.   end;

And the data?
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

balazsszekely

  • Guest
Re: Get and post request to URL
« Reply #3 on: January 24, 2018, 06:59:50 pm »
@torbente

Just take a look at the declaration of Post/SimplePost:
Code: Pascal  [Select][+][-]
  1.    Procedure Post(const URL: string; const Response: TStream);
  2.    Procedure Post(const URL: string; Response : TStrings);
  3.    Procedure Post(const URL: string; const LocalFileName: String);
  4.    function Post(const URL: string) : String;
  5.  
  6.    Class Procedure SimplePost(const URL: string; const Response: TStream);
  7.    Class Procedure SimplePost(const URL: string; Response : TStrings);
  8.    Class Procedure SimplePost(const URL: string; const LocalFileName: String);
  9.    Class function SimplePost(const URL: string) : String;
  10.  

Now you can decide which method do you want to use. Also take a look at this: http://forum.lazarus.freepascal.org/index.php/topic,39638.msg272761.html#msg272761

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Get and post request to URL
« Reply #4 on: January 24, 2018, 07:05:15 pm »
http://forum.lazarus.freepascal.org/index.php/topic,39638.msg272761.html#msg272761

That example was enough. Thanks again.

EDIT: Upps! The url is HTTPS and now i get a "invalid protocol:HTTPS" error. Any workaround?
« Last Edit: January 24, 2018, 07:15:58 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Get and post request to URL
« Reply #5 on: January 24, 2018, 07:17:44 pm »
http://forum.lazarus.freepascal.org/index.php/topic,39638.msg272761.html#msg272761

That example was enough. Thanks again.

EDIT: Upps! The url is HTTPS and now i get a "invalid protocol:HTTPS" error. Any workaround?
Install OpenSSL, the unit should automatically register HTTPS protocol when it finds the library.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Get and post request to URL
« Reply #6 on: January 25, 2018, 01:33:14 pm »
It worked!

I installed the open ssl in my PC but i still get the same error.
What i did:

- I copy the openssl .dll files in my windows/system and in my app root directory.
- i added the unit openssl.

What else i need do?
« Last Edit: January 25, 2018, 02:49:27 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Get and post request to URL
« Reply #7 on: January 25, 2018, 10:21:27 pm »
- I copy the openssl .dll files in my windows/system and in my app root directory.
Make sure the files are named as written in the interface var section here: https://svn.freepascal.org/svn/fpc/tags/release_3_0_4/packages/openssl/src/openssl.pas
- i added the unit openssl.
Not needed.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Get and post request to URL
« Reply #8 on: January 26, 2018, 01:07:02 am »
libeay32.dll and ssleay32.dll are in many places: windows/system, windows/system32, my app root directory and even in the lazarus directory. Still not working.  :(
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

balazsszekely

  • Guest
Re: Get and post request to URL
« Reply #9 on: January 26, 2018, 05:41:43 am »
Quote
@torbente
libeay32.dll and ssleay32.dll are in many places: windows/system, windows/system32, my app root directory and even in the lazarus directory. Still not working.  :(
Then you most likely use a wrong version of the dlls. Download from here:
   32bit: http://packages.lazarus-ide.org/openssl-1.0.2j-i386-win32.zip
   64bit: http://packages.lazarus-ide.org/openssl-1.0.2j-x64_86-win64.zip

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Get and post request to URL
« Reply #10 on: January 26, 2018, 02:04:42 pm »
Quote
@torbente
libeay32.dll and ssleay32.dll are in many places: windows/system, windows/system32, my app root directory and even in the lazarus directory. Still not working.  :(
Then you most likely use a wrong version of the dlls. Download from here:
   32bit: http://packages.lazarus-ide.org/openssl-1.0.2j-i386-win32.zip
   64bit: http://packages.lazarus-ide.org/openssl-1.0.2j-x64_86-win64.zip

Those were the same files i was using. Anyway, i re-download your files with the same result. I even try with 64 bits even when my PC is a 32 bits system but nothing.

Im using Windows 7 Home Basic, 32 Bits, Running LLazarus V 1.44

So maybe is something with the code?

Code: Pascal  [Select][+][-]
  1. function ReadURL(url:string):string;
  2. begin
  3. with TFPHTTPClient.Create(nil) do
  4. try
  5.   ReadURL:= Get('https://api.bitfinex.com/v1/ticker/btcusd');
  6. finally
  7.   Free;
  8. end;
  9. end;

Im getting a "Invalid Protocol:https" error.

EDIT: I noticed that sometimes the icon of the lazarus IDE is pink and sometimes yellow. Any reason for this?
« Last Edit: January 26, 2018, 02:43:29 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

balazsszekely

  • Guest
Re: Get and post request to URL
« Reply #11 on: January 26, 2018, 04:16:41 pm »
Quote
Im using Windows 7 Home Basic, 32 Bits, Running LLazarus V 1.44
You should update to Lazarus 1.8/FPC 3.04. Any good reason to stay with 1.4?

Quote
So maybe is something with the code?
It's nothing wrong with that code, fphttpclient works great. I use it extensively in my applications, Lazarus IDE also use it. Take a look at the attached screenshot.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Get and post request to URL
« Reply #12 on: January 26, 2018, 05:28:15 pm »
Well, i did not use lazarus for +2 yeras and i really did not believe update lazarus necessary  %)
Any easy way to update it? Im checking the lazarus IDE for an auto-update, and the directory for a file but nothing. I searched the web and look like the best way is manual.
I will update lazarus and the try again.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

balazsszekely

  • Guest
Re: Get and post request to URL
« Reply #13 on: January 26, 2018, 05:51:52 pm »
Since you're on windows it's very easy. Just uninstall the old version, then install the new one with the installer. Make sure you delete the old config files.

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Get and post request to URL
« Reply #14 on: January 26, 2018, 05:53:32 pm »
Best to use the official repositories  or fpcupdeluxe to upgrade.
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018