I can fake a GET in LC. How do I fake a POST in LC

Are you using LiveCode to create server scripts or CGIs?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Thu Mar 15, 2012 5:14 am

Hi all,

I've just been catching up on some research into lcServer and reading about the GET and POST methods.

I see I can fake a Get in Android (I think):
launch url "http://192.168.1.6/lcMLoginGET.lc?UserI ... W=LiveCode"

Is there a way to fake a POST?

Maybe using the binary functions of LC?

or in c#:
http://stackoverflow.com/questions/3463 ... -http-post

Code: Select all

string var1 = "Foo";
string var2 = "Bar";

ASCIIEncoding encoding = new ASCIIEncoding();
string post = "var1=" + var1 + "&var2=" + var2;
byte[] bites = encoding.GetBytes(post);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://Url/PageToPostTo.aspx");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bites.Length;
Stream s = request.GetRequestStream();
s.Write(bites, 0, bites.Length);
s.Close();
Or javascript:

Code: Select all

function makeRequest(message,url,responseFunction){
var http_request;
    if (window.XMLHttpRequest){ // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
        // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
    }
}
else if (window.ActiveXObject){ // IE
    try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e){
        try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
}

http_request.onreadystatechange = responseFunction;
    http_request.open("POST", url);
http_request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
http_request.send(message);
}
Is there a way we could use either of these scripts to fake a POST in LC for Android?
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7233
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by jacque » Thu Mar 15, 2012 7:22 pm

GET and POST are directly supported in mobile apps so you shouldn't have to fake anything. That said, there's a bug in Android POST that has been fixed in the next release. You can work around it in the current release:

Set this header manually using the httpHeaders property:

Code: Select all

set the httpHeaders to "Content-Type: application/x-www-form-urlencoded"
You should set the httpHeaders to empty once the post command has completed so it won't affect any subsequent url access.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Thu Mar 15, 2012 9:36 pm

I'm sure that's not all there is to it.

Is there a sample app or link that I can get an idea of the frame work around this context?

http://forums.runrev.com/viewtopic.php? ... ers#p39387

LiveCode Dictionary: post
post data to URL destinationURL

Summary:
Sends data to a web server using the POST action of HTTP.

Example:

Code: Select all

post myData to URL "http://www.example.net/indications.cgi"
post field "Return Values" to URL field "Current Page"
post tData to URL tMyUrl
Description:
Use the post command to submit data to a web server.

Parameters:

data - Is any text that evaluates to a string.

destinationURL - Is the URL where the data is to be sent.

Comments:
Data you send should be encoded using the URLEncode function.

The value the web server returns is placed in the it variable. If an error occurs, the result function is set to the error message.

The HTTP header sent with the POST request can be changed using either the HTTPHeaders property or the libURLSetCustomHTTPHeaders command. By default, the "Content-Type" header line is set to "application/x-www-form-urlencoded".

Note: Sending data with the post command is a blocking operation: that is, the handler pauses until LiveCode is finished sending the data. Since contacting a server may take some time, due to network lag, URL operations may take long enough to be noticeable to the user.

Important! If a blocking operation involving a URL (using the put command to upload a URL, the post command, the delete URL command, or a statement that gets an ftp or HTTP URL) is going on, no other blocking URL operation can start until the previous one is finished. If you attempt to use a URL in an expression, or put data into a URL, while another blocking URL operation is in progress, the result is set to "Error Previous request not completed".

To send a username and password with the post command, use the standard syntax for including this information in a URL. For example, to access http://www.example.com/ with the username "me" and the password "pass", use the following statement:

post someData to URL "http://me:pass@www.example.com/"

Important! If your user name or password contains any of the characters ":", "@", "/", ".", or "|", use the URLEncode function to safely encode the user name or password before putting them into the URL. The following example constructs a URL for a user whose password contains the "@" character:

Code: Select all

	put "name" into userName
	put "jdoe@example.com" into userPassword
	put "http://" & userName & ":" & URLEncode(userPassword) & "@www.example.net/index.html" into fileURLToGet
	get URL fileURLToGet
Important! The post command is part of the Internet library on desktops. To ensure that the command works in a standalone application, you must include this custom library when you create your standalone. In the Inclusions section of the Standalone Application Settings window, make sure "Internet Library" is selected in the list of script libraries.

Note: When included in a standalone application, the Internet library is implemented as a hidden group and made available when the group receives its first openBackground message. During the first part of the application startup process, before this message is sent, the post command is not yet available. This may affect attempts to use this command in startup, preOpenStack, openStack, or preOpenCard hand in the main stack. Once the application has finished starting up, the library is available and the post command can be used in any handler.

Note: The Android and iOS engines do not support 'libUrl' (as in libURLSetCustomHTTPHeaders) but allow you to use post in the background. When specifying URLs for Android and iOS, you must use the appropriate form that confirms to the RFC standards. Ensure that you urlEncode any username and password fields appropriately for FTP urls.

(italics mine)
----

Code: Select all

put "name" into userName
put "jdoe@example.com" into userPassword
put "http://" & userName & ":" & URLEncode(userPassword) & "@www.example.net/index.html" into fileURLToGet

set the httpHeaders to "Content-Type: application/x-www-form-urlencoded"   --  ???????

get URL fileURLToGet
or perhaps:

Code: Select all

    
on mouseUp
      put urlEncode(field "username") into tUsername
      put urlEncode(field "rating") into tRating
      put urlEncode(field "comment") into tComment
      put libURLFormData("username", tUsername, "rating", tRating, "comment", tComment) into tDataToBePosted

set the httpHeaders to "Content-Type: application/x-www-form-urlencoded"   --  ???????

      post tDataToBePosted to url (field "urlOfWebForm")
      put it into field "displayResult"
end mouseUp
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Thu Mar 15, 2012 11:15 pm

My notes:
http://lessons.runrev.com/s/lessons/m/4 ... ode-mobile

http://forums.runrev.com/viewtopic.php? ... ata#p39387
----

This works on win desktop but not in android.
Any help would be greatly appreciated:

Code: Select all

on mouseUp
   
   put urlEncode(field "txtUserName") into tUsername
   put urlEncode(field "txtPassword") into tPassword
   
   --put libURLFormData("username", tUsername, "password", tPassword) into tDataToBePosted
   --  username=12345&password=123456
      
   put "username=" & tUserName & "&password=" & tPassword into tDataToBePosted

   put "Post:" & cr & tDataToBePosted & cr & cr   into field txtDisplayResult
   
   set the httpHeaders to "Content-Type: application/x-www-form-urlencoded"   --  ???????
   
   post tDataToBePosted to url ("http://192.168.1.6/lcLoginPost.lc")
   put "Reply:" & cr & it after field "txtDisplayResult"
   
end mouseUp
Results on win desktop:
Post:
username=12345&password=123456

Reply:
lcLoginPost<br>Your Username is: 12345<br>Your Password is: 123456<br>
The Results on Android:
Post:
username=12345&password=123456

Reply:
LCLoginPOST.lc

Code: Select all

<?lc

-- http://localhost/lcGet.lc?company=SanctuarySoftware&state=VIC
-- notice ? delimiter to seperate parameter data from web Address
-- notice & delimiter to seperate one paramater from another

     put "lcLoginPost" & "<br>"
     put "Your Username is:" && $_POST["username"] & "<br>"
     put "Your Password is:" && $_POST["password"] & "<br>"

?>

<?lc
    if $_POST["form_submitted"] is true then
          if $_POST["username"] is "runrev" and $_POST["password"] is "1234" then
              put "You have successfully logged in. Welcome to the super secret area."
          else
          put "Your username and password are incorrect. Please try again."
         end if
    end if
?>
Attachments
Login.zip
(59.39 KiB) Downloaded 408 times
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7233
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by jacque » Fri Mar 16, 2012 9:25 pm

Have your scripts check "the result". Any errors will be reported there and may give you some info. If the "it" variable is empty, generally the result will not be.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Fri Mar 16, 2012 10:16 pm

Cool! Thanks for that Jacque.

Now we're getting somewhere:

java.net.socket.exception:Permission denied

Is this the bug you mentioned or is this something else?
Last edited by BarrySumpter on Mon Apr 09, 2012 12:58 am, edited 1 time in total.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7233
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by jacque » Fri Mar 16, 2012 10:44 pm

That's something else, it's coming from the server. The bug was that the default header was wrong and nothing actually got posted (which is why you needed to add the header yourself.) Since you're getting a response, POST is working but the server doesn't like what you sent. I'm not sure why you'd get a permissions error though, or why java is involved.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Fri Mar 16, 2012 11:35 pm

10 *'s jacque.
Really appreciate the support.

will test again on win n c what happens.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Sat Mar 17, 2012 12:41 am

Works ok on windows

a little research suggested this:

Add Internet permission to your manifest:

<uses-permission android:name="android.permission.INTERNET"/>

How would I do this in LC for Android?
Found it:
File | Standalone Application Settings | Android | application Permissions | Internet (ticked on)
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Sat Mar 17, 2012 1:13 am

Yep that solved it.

I was also getting a 'Network is unreachable'
eventhough I could browse with Android default browser without issue.
Also Checked to make sure my
Settings | Wireless Networks | Wi-Fi was turned on

I rebooted both my hd2 and my pc server.


Now getting and posting successfully.

Also:

Code: Select all


   put "?username=" & tUserName & "&password=" & tPassword into tDataToBePosted
   put "&form_submitted=true" after tDataToBePosted
   put "Post:" & cr & tDataToBePosted & cr & cr   into field txtDisplayResult
   
   set the httpHeaders to "Content-Type: application/x-www-form-urlencoded"   --  ???????

   put "http://192.168.1.6/lcLoginGet.lc" & tDataToBePosted into x   --works
   get URL x        -- works

   get URL "http://192.168.1.6/lcLoginGet.lc?username=runrev&password=1234&form_submitted=true"  
   -- works

   get URL "http://192.168.1.6/lcLoginGet.lc" & tDataToBePosted  
   -- CONCAT HERE DOES NOT WORK - LC server doesn't recognise parameters.


Thanks again.
I would have been completely lost.

Now if we could sort out some work around for getting the GPS coordinates in v4.6.4,
I wouldn't have to stress so much about upgrading to 5.5.

As always - Working LC project and LC Server Side Scripting attached
Attachments
Login.zip
(60.08 KiB) Downloaded 405 times
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7233
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by jacque » Sat Mar 17, 2012 7:50 pm

get URL "http://192.168.1.6/lcLoginGet.lc" & tDataToBePosted
-- CONCAT HERE DOES NOT WORK - LC server doesn't recognise parameters.
Try forcing an evaluation by putting everything in parens:

get URL ("http://192.168.1.6/lcLoginGet.lc" & tDataToBePosted )

Otherwise the engine will stop evaluating after the quoted URL.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: I can fake a GET in LC. How do I fake a POST in LC

Post by BarrySumpter » Sat Mar 17, 2012 9:58 pm

Another nice one jacque!

Many thanks.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Post Reply

Return to “CGIs and the Server”