Simulate submitting data from a web browser

From DataFlex Wiki
Revision as of 13:35, 4 March 2020 by Hsymington (talk | contribs) (Added to the How To and Web Programming categories)
Jump to navigationJump to search

httpPostRequest

by Nick Wright

I just spent a while trying to figure the chttpRequest class out and the help has no real world examples so i am posting so as to save somebody the time I wasted. Lets say you need to post to a location e.g. http://www.test.com/testlogin.php and you are trying to simulate filling in forms and posting as in the following html:

 <form action=/testlogin.php method=post>
   Name:  <input type=text name=uid /><br />
   Pass:  <input type=password name=pass /><br />
   <input type=submit name=submit>
 </form>

then this example view works:

 Use cHttpTransfer.pkg
 Use cTextEdit.pkg
     
 Deferred_View Activate_ovwtestLogin for ;
 Object ovwtestLogin is a dbView
   Set Border_Style to Border_Thick
   Set Size to 200 350
   Set Location to 2 2
   Set Maximize_Icon to True
   Set Verify_Data_Loss_Msg to 0
   Set Verify_Exit_Msg      to 0
   Object oForm1 is a Form
     Set Size to 13 100
     Set Location to 15 124
     Set Label to "username"    
   End_Object
   Object oForm2 is a Form
     Set Size to 13 100
     Set Location to 31 124
     Set Label to "password"    
   End_Object
   Object oHttpTransfer1 is a cHttpTransfer
     Set psRemoteHost to "www.test.com"
     
     Property String psAllData
     
     Procedure OnDataReceived String sContentType String sData
       String sAll
     
       Get psAllData to sAll
       Set psAllData to (sAll + sData) 
     End_Procedure
   End_Object
   Object oButton1 is a Button
     Set Location to 58 130
     Set Label to 'Test'
     
     // fires when the button is clicked
     Procedure OnClick
       Send Check_Pass            
     End_Procedure
   End_Object
   Object oTextEdit1 is a cTextEdit
     Set Size to 116 278
     Set Location to 80 47
     Procedure ShowResult
       Address aMyAddress
       String  sText
     
       Get psAllData of oHttpTransfer1 to sText
     
       GetAddress of sText to aMyAddress
       Set paValue to aMyAddress
     End_Procedure  // GetControlContents
   End_Object
   Procedure Check_Pass
     String sPost sUser sPass
     Integer iOK iErr
     
     Set psAllData of oHttpTransfer1 to ''
     Get Value of oForm1 to sUser
     Get Value of oForm2 to sPass
     Move ('uid=' + trim(sUser) + '&pass=' + trim(sPass)) to sPost
     Get AddHeader of oHttpTransfer1 "CONTENT-TYPE" "application/x-www-form-urlencoded" to iErr
     Get HttpPostRequest of oHttpTransfer1 "/testlogin.php" sPost False to iErr
     Send ShowResult of oTextEdit1
   End_Procedure
     
 Cd_End_Object

Note the help documentation in older VDF versions appears to be wrong: parameter sData==== Memory address of the data to be posted the string will do, does not need to be memory address!

External Links

httpPostRequest