Need to get data back from an HTTP POST

I am using an HTTP Poster (HTTP plugin) to post data to a page on our webserver. I have this working fine.

What I cannot figure out how to do is get the data of the resulting page after the POST request! There doesn’t seem to be a method to do this. poster.doPost() returns only the HTTP status code - not the HTML of the resulting page! There is no method of Poster that allows me to retrieve the data the server sends back. And plugins.http.getPageData(…) makes a separate request after the POST operation without the variables I sent, so that’s no good…

HELP! Am I missing how to do this, or could this functionality be added? It seems to me that the hard part of making the POST request has already been handled; for me, though, the results of the operation are the important data I’m trying to get at.

try poster.getPageData() after the post.

Nope, I get an error:

getPageData is not a function.

Nice try, though. In case it matters, here is text from the about box:

Servoy Developer
Version R2 2.2.4-build 336
Java version 1.4.2-54 (Mac OS X)

PROBLEM SOLVED!

I found the post about using the HTTP plugin from Servoy 3.0 with Servoy 2.x. I even figured out how to download commons-httpclient-3.0 from Jakarta. Once I did both of these steps, the http plugin appeared properly in the Method Editor.

The getPageData method finally showed up under Poster, but then I got: java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException when I called getPoster. So, I downloaded commons-codec-1.3.jar and placed it in my lib folder. Restarted Servoy for a third time and everything worked GREAT.

Great work on the plugin! Jan Blok, I owe you a beer when I make it to Amsterdam. I nearly had a heart attack when I found out that it was acceptance of a session cookie, and not the lack of HTTP POST, that was holding me back on this project. I kept my fingers crossed that the new plugin would support this and you didn’t let me down. createHttpClient is awesome!

Thank you for sharing this information. Much appreciated!

Hi Amisle,

can you explain, what you could do with that?
some practical examples?

Well, HJK, I needed all of this so I could fetch a list of albums from our Gallery server. It is a PHP-based photo gallery. Gallery comes with a Java program called Gallery Remote, and the protocol is pretty well documented (Google “gallery_remote2.php protocol”) and very easy to understand. In the end, the photos are stored in a directory on the webserver where they can be accessed by URL.

I wanted to tap into this already-established hirerarchy rather than storing duplicate photos in our database; it requires much less space to store a photo’s URL in the database than all the binary data, and any changes would only have to be made in one place.

To get the list of albums, then the images in each album, and finally the URL of the selected image, I needed to tap into the Gallery Remote engine. However, to do this, several HTTP POST operations must be conducted and if your Gallery requires a login, you must accept a session cookie that signifies you have logged in. Neither of these operations were possible with the Servoy 2.x HTTP plugin since I could not store a session key (making it impossible to login) or read the page resulting from a POST operation (making it impossible to read the album/image data the server sent back).

If there is sufficient interest, I may release my Gallery Integration module in the coming months…

As for other applications, I think the possibilities are endless; use your imagination. Amazon or eBay integration? Probably possible now with sufficient coding.

If there is sufficient interest, I may release my Gallery Integration module in the coming months…

Yeah, I would love to see some coding.

Another interesting use is a fully automated way to push new versions of solutions into production.

examples, examples please? :)

Doing http posts is mainly a matter of reading html source code and knowing how to use Servoy’s debugger. Here’s a working version of a solution pusher, NOTE that in real life you’d put in way more error handling but to keep it simple and readible I’ve left out as much overhead handling as possible.

 var L_Poster, L_HttpResult, L_HttpCode, L_TmpFile,L_TmpSiNo ;
 L_TmpFile = plugins.file.convertStringToJSFile('C:/Servoy/22/solutions/test.servoy');
 L_TmpSiNo = L_TmpFile.exists();
 if ( L_TmpSiNo == true ) 
    {
      L_Poster     = plugins.http.getPoster('http://localhost:8080/servoy-admin/solutions/import');
      L_AddParam1  = L_Poster.addFile('if','file',L_TmpFile);
      L_AddParam2  = L_Poster.addParameter('submit','Import!');
      L_AddParam2  = L_Poster.addParameter('up','1');
      L_AddParam2  = L_Poster.addParameter('ms','0')
      L_AddParam2  = L_Poster.addParameter('of','1');
      L_AddParam2  = L_Poster.addParameter('df','1');
      L_HttpCode   = L_Poster.doPost('test', 'test'); //username/passw
      L_HttpResult = L_Poster.getPageData();
      if (L_HttpResult == "OK" || L_HttpResult == "Ok" )
          { // succesful, delete tmp files, or other process handling here
            
          }
      else
         {  // an error occurred, error handling here
          
         }
}

amisle,

I see that you refer to createHttpClient. I have a problem similar to yours, but I can’t understand how to create an Http Client. As you know there are no docs available and I am a bit (a lot?) confused. Can you explain, please, how do you use it? Thanks

Hi,
I am trying to do a POST to a web site of some data (not a file), instead of a GET (it is to handle credit card transactions using PROTX). I had used getPageData which of course did not work and then saw this post. I tried using Jan’s sample code (with a couple of tweaks), but can’t seem to get the resulting pageData back after doing the POST (which returns a value of 200, which I think means OK, & checking on the web site, it seems the the POST worked!!! :D )

Please could someone tell me what is wrong with this code (I’m not sure I need all the 'escape’s in the code which were there when I was trying to pass all the data in a URL with GET…)

//setUpDataForPreAuthProtx
//set up fields for protx

var L_Poster, L_HttpResult, L_HttpCode, L_AddParam2 ; 

L_Poster  = plugins.http.getPoster('https://ukvpstest.protx.com/VPSDirectAuth/PaymentGateway.asp'); 

L_AddParam2 = L_Poster.addParameter('VPSProtocol','2.22'); 
L_AddParam2 = L_Poster.addParameter('TxType','PREAUTH'); 
L_AddParam2 = L_Poster.addParameter('Vendor','beerchoice') 
L_AddParam2 = L_Poster.addParameter('VendorTxCode',escape( orders_id )); 
L_AddParam2 = L_Poster.addParameter('Amount','1.00'); 
L_AddParam2 = L_Poster.addParameter('Currency','GBP') ;
L_AddParam2 = L_Poster.addParameter('Description',escape( orders_to_orders_products.products_model )) ;
L_AddParam2 = L_Poster.addParameter('CardHolder',escape( cc_owner )) ;
L_AddParam2 = L_Poster.addParameter('CardNumber',escape( plugins.fmp.substitute( cc_number, ' ', '') )) ;
L_AddParam2 = L_Poster.addParameter('StartDate',plugins.fmp.left( cc_start , 2) + plugins.fmp.right( cc_start , 2)) ;
L_AddParam2 = L_Poster.addParameter('ExpiryDate',plugins.fmp.left( cc_expires , 2) + plugins.fmp.right( cc_expires , 2)) ;
L_AddParam2 = L_Poster.addParameter('IssueNumber',cc_issue_no) ;
L_AddParam2 = L_Poster.addParameter('CV2',cc_cv2) ;
L_AddParam2 = L_Poster.addParameter('CardType',cc_type) ;
L_AddParam2 = L_Poster.addParameter('BillingAddress',escape( billing_street_address + ', ' + billing_suburb + ', ' + billing_city + ', ' + billing_state )) ;
L_AddParam2 = L_Poster.addParameter('BillingPostCode',escape( billing_postcode )) ;
L_AddParam2 = L_Poster.addParameter('DeliveryAddress',escape( delivery_street_address + ', ' + delivery_suburb + ', ' + delivery_city + ', ' + delivery_state )) ;
L_AddParam2 = L_Poster.addParameter('DeliveryPostCode',escape( delivery_postcode )) ;
L_AddParam2 = L_Poster.addParameter('CustomerName',escape( customers_name )) ;
L_AddParam2 = L_Poster.addParameter('ContactNumber',escape( customers_telephone )) ;
L_AddParam2 = L_Poster.addParameter('ContactFax','') ;
L_AddParam2 = L_Poster.addParameter('CustomerEMail',escape( customers_email_address )) ;
L_AddParam2 = L_Poster.addParameter('ApplyAVSCV2','3') ;

L_HttpCode = L_Poster.doPost() ;

globals.g_cc_recvd_text = plugins.http.getPageData(L_Poster);

Thanks,
Rafi.

Rafig,

I think that you should give each parameter a different number (addParameter2, addParameter3) aso.

I had a problem similar to yours. I received a 200 but the parameters were wrong.

Hi rioba,
thanks for your reply

I think that you should give each parameter a different number (addParameter2, addParameter3) aso.

I had a problem similar to yours. I received a 200 but the parameters were wrong.

The addParam variable just get a value of TRUE (or FALSE I guess), as I debugged the method and each one just returned TRUE. Checking the site I am POSTing to, it looks like my POST is going OK, but I need to get the returned page data. How did you do that?

It is true it returns a boolean, but the parameter is sent to the server as POST, so the parameters must have different names and be ordered according to the order the server expects to receive them. You should also check the reply from the server. I used FireFox pageinfo to do that. This what I have done and it worked.

you have to use the poster object:
globals.g_cc_recvd_text = L_Poster.getPageData();

Hi Jan,

I’ve been trying out this code in V3 developer. WIN XP, Java 1.5

jaleman:
Here’s a working version of a solution pusher, NOTE that in real life you’d put in way more error handling but to keep it simple and readible I’ve left out as much overhead handling as possible.

 var L_Poster, L_HttpResult, L_HttpCode, L_TmpFile,L_TmpSiNo ;

L_TmpFile = plugins.file.convertStringToJSFile(‘C:/Servoy/22/solutions/test.servoy’);
L_TmpSiNo = L_TmpFile.exists();
if ( L_TmpSiNo == true )
{
L_Poster = plugins.http.getPoster(‘http://localhost:8080/servoy-admin/solutions/import’);
L_AddParam1 = L_Poster.addFile(‘if’,‘file’,L_TmpFile);
L_AddParam2 = L_Poster.addParameter(‘submit’,‘Import!’);
L_AddParam2 = L_Poster.addParameter(‘up’,‘1’);
L_AddParam2 = L_Poster.addParameter(‘ms’,‘0’)
L_AddParam2 = L_Poster.addParameter(‘of’,‘1’);
L_AddParam2 = L_Poster.addParameter(‘df’,‘1’);
L_HttpCode = L_Poster.doPost(‘test’, ‘test’); //username/passw
L_HttpResult = L_Poster.getPageData();
if (L_HttpResult == “OK” || L_HttpResult == “Ok” )
{ // succesful, delete tmp files, or other process handling here

      }
  else
     {  // an error occurred, error handling here
      
     }

}

The script executes, the servoy file is correctly added to the solution, confirmed in the repository (BTW, not the current solution), and I get the PageData back ok.

I notice some odd behavior after running it. The only change made do your method is the path to the .servoy file.

  1. Although the method has completed, the Stop button in the debug window remains ‘active’
  2. I can no longer go into designer mode - the icon in the form editor window is disabled.
  3. If I click on Open Solution, I don’t get the usual dialog “There is currently an open solution, close and continue?” It just goes straight to the “Open Solution” dialog, as if there’s no solution open.

(see the screen shot for an illustration of 1-3)

  1. Same thing with Repository - no warning that there’s a solution open, it goes straight to the respository dialog
  2. ‘Close’ on Menu doesn’t work.
  3. application.closeSolution() in a mthod doesn’t work
  4. Opening any solution after importing to the repository (eg _START_HERE) now takes ~45 -60 seconds, instead of the usual ~1 second. Opening the same solution a second time is rapid.

After the second opening of the solution, everything is back to normal.

There are no error messages in the console.

It seems that the process of pushing a new solution into the repository is having some bad effects. Could you shed some light?

It seems not to work well with Servoy Developer, Servoy Server should work the same as manual post.

Hi Jan, I was delighted to find it in V3 developer and hope it will work, as most of my customers will need Servoy Offline (=Disabled Developer)