Recent

Author Topic: [Help] Rest API programming.  (Read 27560 times)

shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Rest API programming.
« Reply #15 on: November 12, 2015, 09:04:27 pm »
Okay hi and good evening ,

Earlier on I was on Mobile so chances of coming on the computer was limited, hence i decided to  come online and write on here as i have the time here.
Now am trying to simulate a simple HTTP Request with Wininet i wanted to be sure of what I am Doing hence i decided to come up with a source code , I didnt fill other parameters because i wanted to see how it would be placed when Simulating(Sincerely pardon me as this is my first time Handling something like this)

Code goes like this
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode delphi}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes , Wininet , Windows
  10.   { you can add units after this };
  11.  
  12. procedure PostData()
  13. var
  14.   hInet: HINTERNET;
  15.   hHTTP: HINTERNET;
  16.   hReq: HINTERNET;
  17.   const
  18.     accept: packed array[0..1] of LPWSTR = (PChar('*/*'), nil);
  19.     header: string = 'Content-Type: application/x-www-form-urlencoded';
  20. begin
  21.   hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  22.      hHTTP := InternetConnect(hInet, PChar(Server), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 1);
  23.       hReq := HttpOpenRequest(hHTTP, PChar('POST'), PChar(Resource), nil, nil, @accept, 0, 1);
  24.       if not HttpSendRequest(hReq, PChar(header), length(header), PChar(Data), length(Data)) then
  25.       raise Exception.Create('HttpOpenRequest failed. ' + SysErrorMessage(GetLastError));
  26.       InternetCloseHandle(hReq);
  27.       InternetCloseHandle(hHTTP);
  28.       InternetCloseHandle(hInet);
  29. end
  30.  
  31.  
  32. begin
  33. end.
  34.  
  35.  

Now In the case of the parameters i used Live HTTP Headers to retrieve the headers as well so i got this

Code: Pascal  [Select][+][-]
  1. https://perfectmoney.is/api/step1.asp
  2.  
  3. POST /api/step1.asp HTTP/1.1
  4. Host: perfectmoney.is
  5. User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
  6. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  7. Accept-Language: en-US,en;q=0.5
  8. Accept-Encoding: gzip, deflate
  9. Referer: https://perfectmoney.is/api/pay.asp?acc=&cur=&name=&email=&memo=&pid=&ref=https%3A%2F%2Fperfectmoney.is&amount=250&action=Make+payment
  10. Cookie: details=1366x768; data=289775471; pmc=b76ede0e957fd5ca7e6b03beb9949234; PM_SESSION=9e58e6742c8adebcb8c1ab00a2026e7603ed02108b9858797427819ce93efe91d4fa0a74
  11. Connection: keep-alive
  12. Content-Type: application/x-www-form-urlencoded
  13. Content-Length: 257
  14. PAYEE_ACCOUNT=&PAYEE_NAME=&PAYMENT_UNITS=&STATUS_URL=mailto%3A&PAYMENT_URL=https%253A%252F%252Fperfectmoney.is&PAYMENT_URL_METHOD=LINK&NOPAYMENT_URL=https%253A%252F%252Fperfectmoney.is&NOPAYMENT_URL_METHOD=LINK&SUGGESTED_MEMO=&PAYMENT_ID=&PAYMENT_AMOUNT=250
  15.  
  16.  

Now Good sir, How do I place the Values and do this in Pascal? suppose this was C / C++ I could have tried something , but i really would like to do this in Pascal, kindly help.

Help would be much much appreciated :)
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: [Help] Rest API programming.
« Reply #16 on: November 12, 2015, 09:19:45 pm »
First of all your code does absolutly nothing. You just have a "begin end".

Secondly... how did you come by https://perfectmoney.is/api/step1.asp ?

If I look at the documentation you could use https://perfectmoney.is/acct/verify.asp or https://perfectmoney.is/acct/confirm.asp.

I'm not that familiar with wininet myself but I'll show you some code using Synapse:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btVerifyClick(Sender: TObject);
  2. var
  3.   HTTP: THTTPSend;
  4.   Url: string;
  5.   UrlData: string;
  6.   Data: TMemoryStream;
  7. begin
  8.   if (edAccountID.Text = '') or (edPassPhrase.Text = '') or
  9.     (edPayer.Text = '') or (edPayee.Text = '') or (edAmount.Text = '') or
  10.     (edMemo.Text = '') then
  11.   begin
  12.     ShowMessage('Please fill out the fields with *');
  13.     exit;
  14.   end;
  15.  
  16.   UrlData := 'AccountID=' + edAccountID.Text;
  17.   UrlData += '&PassPhrase=' + edPassPhrase.Text;
  18.   UrlData += '&Payer_Account=' + edPayer.Text;
  19.   UrlData += '&Payee_Account=' + edPayee.Text;
  20.   UrlData += '&Amount=' + edAmount.Text;
  21.   UrlData += '&Memo=' + edMemo.Text;
  22.   if edPaymentID.Text <> '' then
  23.     UrlData += '&PAYMENT_ID=' + edPaymentID.Text;
  24.   if edcode.Text <> '' then
  25.     UrlData += '&code=' + edcode.Text;
  26.   if edperiod.Text <> '' then
  27.     UrlData += '&period=' + edperiod.Text;
  28.  
  29.   Url := 'https://perfectmoney.is/acct/verify.asp';
  30.  
  31.   Data := TMemoryStream.Create;
  32.   try
  33.     if HttpPostURL(Url, UrlData, Data) then
  34.     begin
  35.       Data.Position := 0;
  36.       memoResult.Lines.LoadFromStream(Data);
  37.     end;
  38.   finally
  39.     Data.Free;
  40.   end;
  41.  
  42. end;

This should works to send an amount from one account to another.
(use verify.asp to check if everything is ok and confirm.asp to actually send the money)

You'll need to fill in an AccountID, passphrase belonging to that id, payer to which account and payee from which account and the amount.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Rest API programming.
« Reply #17 on: November 12, 2015, 09:32:49 pm »
Grateful Sir, Synapse Works on Lazarus Pascal?
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: [Help] Rest API programming.
« Reply #18 on: November 12, 2015, 09:39:56 pm »
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Rest API programming.
« Reply #19 on: November 13, 2015, 07:46:38 am »
Many thanks @rvk, @thaddy @eny and even everyone who talked on this topic yesterday.

Now I want to install synapse. Do I just find the .pas file and include in my project? I have never used synapse before. Please how do I go about the installation

When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [Help] Rest API programming.
« Reply #20 on: November 13, 2015, 09:27:37 am »
Many thanks @rvk, @thaddy @eny and even everyone who talked on this topic yesterday.

Now I want to install synapse. Do I just find the .pas file and include in my project? I have never used synapse before. Please how do I go about the installation
Read, read, read and read: http://synapse.ararat.cz/doku.php/public:howto:installsynapse

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: [Help] Rest API programming.
« Reply #21 on: November 13, 2015, 09:49:13 am »
Now I want to install synapse. Do I just find the .pas file and include in my project? I have never used synapse before. Please how do I go about the installation
Read, read, read and read: http://synapse.ararat.cz/doku.php/public:howto:installsynapse
@Leledumbo, to be fair... the method on that page doesn't seem to be working anymore (for me at least). (And I'm talking about the directory method because I can't imagine all the synapse files in your own project directory)

There is no "Aditional source search path for all projects" option anymore in the global Codetools options.
(Am I seeing that wrong or is that option completely removed?)

Secondly... adding the synapse directory to the Library path of your project also doesn't work because you'll get an error that you need to use the laz_synapse.lpk package.
Quote
Fatal: Cannot find ssl_openssl used by Unit1. Check if package laz_synapse is in the dependencies of the Project Inspector.

Here is how I normally do it:
  • Download Synapse. I normally advice using the svn version. You can download it on this page. You can use the Download Snapshot button on that page.
  • Extract it to a logical-named and placed directory.
  • Now you need to add laz_synapse.lpk package to your project as a requirement:
    • Package > Open Package File > browse to the synapse directory
    • Choose laz_synapse.lpk
    • Now here is an extra step I always do if I need ssl_openssl
      If ssl_openssl.pas is not yet in the laz_synapse.lpk:
      • choose Add > Add Files from Filesystem
      • Choose ssl_openssl.pas
    • Choose Use >> Add to Project (and close the Package window)
  • Now you can add httpsend, ssl_openssl and any other synapse unit you might need to your uses clause.


shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Rest API programming.
« Reply #22 on: November 13, 2015, 10:29:32 am »
Thanks boss RVK

Lemme do it and get back to you this evening.
Many thanks again.
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: [Help] Rest API programming.
« Reply #23 on: November 13, 2015, 10:54:09 am »
I fiddled some with the options and you could also get away with just adding the source-directory of synapse to the "Other unit files" in your project options. That initially didn't work for me because I still had a loaded laz_synapse in my IDE but after resetting this all it worked.

So it's not adding the directory to the library path but to the "Other unit files" which works too.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Rest API programming.
« Reply #24 on: November 13, 2015, 08:40:03 pm »
Good evening

I have a pressing issue now.
I have downloaded and extracted Synapse. I tried running the code and it tells me

Code: [Select]
ssl_openssl.pas(95,3) Fatal: Can not find unit blcksock used by ssl_openssl.

But i have the .pas file installed . :-\

What could be wrong?
My source Code looks like this Now

Code: Pascal  [Select][+][-]
  1. unit pmstore;
  2.  
  3. {$mode delphi}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ssl_openssl, httpsend;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Edit1: TEdit;
  19.     Edit2: TEdit;
  20.     Edit3: TEdit;
  21.     Edit4: TEdit;
  22.     Label1: TLabel;
  23.     Label2: TLabel;
  24.     Label3: TLabel;
  25.     Label4: TLabel;
  26.     procedure Button1Click(Sender: TObject);
  27.     procedure Edit1Change(Sender: TObject);
  28.   private
  29.     { private declarations }
  30.   public
  31.     { public declarations }
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.lfm}
  40.  
  41. { TForm1 }
  42.  
  43. procedure TForm1.Button1Click(Sender: TObject);
  44. var
  45.     HTTP: THTTPSend;
  46.     Url: string;
  47.     UrlData: string;
  48.     Data: TMemoryStream;
  49. begin
  50. UrlData := 'PAYEE_ACCOUNT=' + Edit1.Text;
  51. UrlData := 'PAYEE_NAME=' + Edit2.Text;
  52. UrlData := 'PAYMENT_UNITS=' + Edit3.Text;
  53. UrlData := ' PAYMENT_AMOUNT=' + Edit4.Text;
  54.  
  55. Url := 'https://perfectmoney.is/acct/confirm.asp';
  56.  
  57. Data := TMemoryStream.Create;
  58.  try
  59.         if HttpPostURL(Url, UrlData, Data) then
  60.         begin
  61.         Data.Position := 0;
  62.         //dont have an idea on how to send
  63.         end;
  64.         finally
  65.           Data.Free
  66.                 end;
  67. end;
  68.  
  69. end.
  70.  
  71.  

Seems it has to be about the translation from Delphi to Lazarus? or what?
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: [Help] Rest API programming.
« Reply #25 on: November 13, 2015, 08:46:11 pm »
How did you include synapse in your project?
Did you add the laz_synapse package to your project or added the synapse directory to the "Other unit files" in your project options?

Also noticed a small problem with your code which you will run into later:
Code: Pascal  [Select][+][-]
  1. UrlData := 'PAYEE_ACCOUNT=' + Edit1.Text;
  2. UrlData := 'PAYEE_NAME=' + Edit2.Text;
  3. UrlData := 'PAYMENT_UNITS=' + Edit3.Text;
  4. UrlData := ' PAYMENT_AMOUNT=' + Edit4.Text;
This just overwrites UrlData on each line.
You want to add all those data together on one line separated by &-sign.
(notice I uses += in my code to add a string to UrlData)

shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Rest API programming.
« Reply #26 on: November 13, 2015, 08:52:35 pm »
How did you include synapse in your project?
Did you add the laz_synapse package to your project or added the synapse directory to the "Other unit files" in your project options?

Also noticed a small problem with your code which you will run into later:
Code: Pascal  [Select][+][-]
  1. UrlData := 'PAYEE_ACCOUNT=' + Edit1.Text;
  2. UrlData := 'PAYEE_NAME=' + Edit2.Text;
  3. UrlData := 'PAYMENT_UNITS=' + Edit3.Text;
  4. UrlData := ' PAYMENT_AMOUNT=' + Edit4.Text;
This just overwrites UrlData on each line.
You want to add all those data together on one line separated by &-sign.
(notice I uses += in my code to add a string to UrlData)

I didnt see the second option you gave. hence i didnt use the other unit files. i just added that laz_synapse.lpk and it threw that error.
I'm using Lazarus 1.2.6

concerning the UrlData, how do i fix that?
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: [Help] Rest API programming.
« Reply #27 on: November 13, 2015, 08:57:28 pm »
I didnt see the second option you gave. hence i didnt use the other unit files. i just added that laz_synapse.lpk and it threw that error.
I'm using Lazarus 1.2.6
1.2.6? That one is quite old. Why are you using such an old version?
You could remove laz_synapse as requirement and add the synapse directory to the "Other unit files" in your project options>Paths.

Quote
concerning the UrlData, how do i fix that?
Well, use my exact example :)

I would also use verify.asp instead of confirm.asp in your testing phase. In that case you get the exact same result but no funds are transferred away from your account.

shonay

  • Full Member
  • ***
  • Posts: 169
Re: [Help] Rest API programming.
« Reply #28 on: November 13, 2015, 09:04:39 pm »
I didnt see the second option you gave. hence i didnt use the other unit files. i just added that laz_synapse.lpk and it threw that error.
I'm using Lazarus 1.2.6
1.2.6? That one is quite old. Why are you using such an old version?
You could remove laz_synapse as requirement and add the synapse directory to the "Other unit files" in your project options>Paths.

Quote
concerning the UrlData, how do i fix that?
Well, use my exact example :)

I would also use verify.asp instead of confirm.asp in your testing phase. In that case you get the exact same result but no funds are transferred away from your account.

Seems you dont get this same issues , which version of Lazarus are u using Sir, hence I think i have to switch
When the power of love overcomes the love of power, the world would know Peace

- Jimi Hendrix.

rvk

  • Hero Member
  • *****
  • Posts: 6948
Re: [Help] Rest API programming.
« Reply #29 on: November 13, 2015, 09:07:12 pm »
Seems you dont get this same issues , which version of Lazarus are u using Sir, hence I think i have to switch
I use Lazarus development version (trunk) and Lazarus 1.4.4 for testing.

Lazarus 1.4.4 (the latest downloadable stable version) will work fine for you.

 

TinyPortal © 2005-2018