Performing a POST under iOS not returning a value?

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Performing a POST under iOS not returning a value?

Post by kcorey » Tue Jan 17, 2012 5:52 pm

Hi All,

I'm trying to use the POST command to post data to my web server:

Code: Select all

on mouseUp
  ask password "login:" with "your password" titled "System Password"
  put "password=" into tData
  put it after tData
  post tData to URL "http://mywebsite.com/login"
  answer "the result is '"&the result&"'"
end mouseUp
A couple strange problems...
1) the password is not encrypted on the iPad (though it is on the PC). I thought this was due to the SSL not being included, but now I'm not sure.
2) the return code seems to be failing on the iPad.

If run from my Mac, the answer dialogue appears saying:
the result is 'error 405 Method Not Allowed'
(this is expected because I haven't configured my server to respond to that url yet).

However, if run from the iPad simulator I can see the request hitting the server, but the dialogue box says:
the result is''

The dictionary entry for "post" mentions the 'Internet library' for desktops, but doesn't mention anything for the iOS or Mobile version.

Is this another interface where iOS devices get a custom handler?

Thanks,

-Ken

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

Re: Performing a POST under iOS not returning a value?

Post by jacque » Tue Jan 17, 2012 7:15 pm

The internet library is not part of mobile builds, but POST and GET have been implemented internally in the engine so they will work. There is no separate command, what you are using is correct.

But the result will only contain content if there is an error. Otherwise the return value is placed in the "it" variable. It would seem logical that your server would return an error no matter what the calling device is, but check "it" to see if anything comes back in that. For example:

answer "Result:" && the result && "It:" && it
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Performing a POST under iOS not returning a value?

Post by kcorey » Wed Jan 18, 2012 12:02 am

Hi Jacque, thanks for writing.

When I added the line:

Code: Select all

answer "the it is '"&it&"'"
On the desktop as before I get "the result is 'error 405 Method Not Allowed'", and "the it is 'OK'". (Which is rather strange, as the POST is actually being returned a '405 Method not allowed' response.)

On the iPad simulator and on the real device I don't get anything either in 'the result' or in 'it'.

So, did I call the post incorrectly?

Incidentally, I've just added the code to handle the request on the server and to log the arguments sent to the server.

On the desktop, the result is '', and 'it' is 'OK'.

On the simulator, the arguments are successfully passed to the server, but I still don't get a response.

Surely I can't be the first person to ever use the get and post commands from iOS...

-Ken

P.S. I'm wondering is there some other form of response to get/post under iOS? An event code or some such?

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

Re: Performing a POST under iOS not returning a value?

Post by jacque » Wed Jan 18, 2012 1:22 am

That's kind of strange. The "OK" is what you usually get when the answer dialog is returning your button selection. You're calling the POST command correctly, and there isn't an alternative way. Rather than use the answer dialog, try a test stack that puts the return values into a field instead, just so you can see what comes back. That will take one extra thing out of the equation at least.

I did some quick tests today and I'm getting very inconsistent results even in the IDE. Like you, I am seeing empty for both the result and "it" about two-thirds of the time. Now and then the real content comes back. So I think there's a problem with POST on more than just mobile, but I don't know exactly what. I fiddled with the timeout interval a little but it didn't make any difference.

Is anyone else out there having inconsistent results with POST in the latest engine?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Performing a POST under iOS not returning a value?

Post by kcorey » Wed Jan 18, 2012 12:48 pm

Ouch! Proper functioning of GET/POST is fairly fundamental to my app.

I hope there's an easy fix available.

-Ken

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Performing a POST under iOS not returning a value?

Post by bangkok » Thu Jan 19, 2012 9:23 am

jacque wrote: Is anyone else out there having inconsistent results with POST in the latest engine?
Everything is fine for me : Windows XP, IDE and standalone, LiveCode 5.02... POST to a LiveCode Server script. I got the answer from the server every time.

If I click on the button very quickly, several times, I got an error (in "it") : "error Previous request not completed".


I have an idea regarding the first question :

Code: Select all

  post tData to URL "http://mywebsite.com/login"
Would it be better to complete the URL, like :

Code: Select all

  post tData to URL "http://mywebsite.com/login.php"

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Performing a POST under iOS not returning a value?

Post by Mark » Thu Jan 19, 2012 11:27 am

Hi Ken,

You can't just add the line "answer it" or similar, because the previous answer command replaces the contents of "it". You need to find a different way. Less confusing is probably

Code: Select all

on mouseUp
  ask password "login:" with "your password" titled "System Password"
  put "password=" into tData
  put it after tData
  post tData to URL "http://mywebsite.com/login"
  put "result:" && the result & cr & "it:" && it
end mouseUp
which puts the result and it into the message box. Additionally (just in case), if your URL consists of multiple parts, put parentheses around the parts:

Code: Select all

post tData to URL ("http://mywebsite.com/" & myVariables)
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Performing a POST under iOS not returning a value?

Post by kcorey » Thu Jan 19, 2012 1:05 pm

I take your point completely, the code below shows this:

Code: Select all

on mouseUp
   if the platform is "iphone" then
      iphoneActivityIndicatorStart "whiteLarge" 
   end if
   
   ask password "Please log in:" with "Your Password" titled "System Password"
   put it into itData
   put the result into resData
   answer "the password is '"&itData&"'"
   put "password=" into tData
   put itData after tData
   
   post tData to URL "http://humdiet.com/login.html"
   put it into postit
   put the result into postResult
   
   answer "the result is '"&postResult&"' the it is '"&postit&"'"
   
   if the platform is "iphone" then
      iphoneActivityIndicatorStop
   end if
   
end mouseUp
The results are interesting. If the server is running, and the page is configured, I'm told that the mobile devices can't encrypt the password (fair enough), and then they all respond as the server is directing them.

If there's a problem, though:

Server not running
------------------
ios - the result is 'Could not connect to the server.' the it is ''

android - the result is 'timeout' the it is ''

mac - the result is 'error socket is not open' the it is ''

pc - the result is 'error Error 10061 on socket' the it is ''

no such page (server returns a 405)
-------------
ios - the result is '' the it is ''

android - the result is 'Method Not Allowed' the it is ''

mac - the result is 'error 405 Method Not Allowed' the it is 'None'

PC - the result is 'error 405 Method Not Allowed' the it is 'None'



The problem to my way of thinking is that the error messages seem distinct on each platform.

For that matter, how do I get to the details of the request? I'd like to be able to find out the return code is '200' or '405' or whatever, and that my cookies have been set to "X", or how many bytes were returned (on a mobile device I might choose to ignore data longer than 32Kb, to save network bandwidth, etc). Is this exposed inside LiveCode?

-Ken

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Performing a POST under iOS not returning a value?

Post by Mark » Thu Jan 19, 2012 1:19 pm

Ken,

You don't understand. The syntax

Code: Select all

 put it into itData
 put the result into resData
clears the result before you put the result into a variable. Switch the two lines!!!

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Performing a POST under iOS not returning a value?

Post by kcorey » Thu Jan 19, 2012 2:12 pm

I appreciate the tip, but the dictionary says neither 'it' nor 'the result' is affected by the put command, so switching those two lines shouldn't affect anything.

Indeed, after testing, I changed the order of the two lines, and am still seeing the problematic behaviour in the post command on iOS.

I guess most of this would go away if there's a way on mobile devices to find out the nitty gritty about the http request. 'it' and 'the result' are handy shortcuts, but they don't really cut it by themselves for web work. I have tried using the libURL stuff, but of course that doesn't exist on mobile devices.

-Ken

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Performing a POST under iOS not returning a value?

Post by Mark » Thu Jan 19, 2012 2:33 pm

Ken,

The post command works fine for me on iOS but not on Android. I think there really is a mistake somewhere. While I'm looking at your script, let me tell you there's a mistake in the line

Code: Select all

   answer "the result is '"&postResult&"' the it is '"&postit&"'"
An ampersand is missing, which is why you get a very strange "result".

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Performing a POST under iOS not returning a value?

Post by Mark » Thu Jan 19, 2012 2:43 pm

Ken,

Reducing your script to the essentials, I get:

Code: Select all

on mouseUp
     put "password=123" into tData     
     post tData to URL "http://humdiet.com/login.html"
     put the result into postResult
     put it into postIt
     answer "result:" && postResult & cr & "it:" && postIt
end mouseUp
When I run the script, the answer dialog says:

result:
it: F


which is correct. Firefox returns exactly the same, so I know that my script is correct.

Are you aware that the ask password command is only useful if you have a mcEncrypted password saved somewhere for comparison? I don't think this is going to work with websites. You can't use the ask password command here.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Performing a POST under iOS not returning a value?

Post by kcorey » Thu Jan 19, 2012 4:20 pm

Thanks for all your help, Mark, I do appreciate you taking the time!

Back a few posts where I showed the results, I'd stated that when it's all set up properly things seem to work. That isn't really the issue.

The issue is that when there's an error, I don't (yet) see how to identify in a reliable way what the error is.

In my case, I was running a script against a page that wasn't there, and that I expected to fail.

On most platforms it failed with a variant on "405 method not allowed" put into the result, which is sorta what the dictionary says (though the variation in the error code makes it problematic to handle well).

On iOS (5.0 (9A334) in both the simulator and my iPad 2) it doesn't. "the result" is empty, and "it" is empty. This is a bug. It should say something in "the result" here.

The best solution, methinks, would be to provide LiveCode scripts a way to access the internals of the response (the code, the cookies, the headers, etc). That way, I simply check to see if the code handed back from the server is a 405, a 404, etc, and have a chance to tell my user something useful. I don't have to try to parse the various versions of error messages.

Trying to match LiveCode semantics perhaps get or a post should return a tuple similar to a loc, for example:
(code,errorMessages,cookies, headers, resultString)

That way I could check

Code: Select all

get URL "http://blah"
put the result into pResponse
if item 1 of pResponse = 200 then
  -- do something with the success
  put "The page is "&(item 5 of pResponse)
else
  -- respond to the failure
  put "The server reported an error of:"&(item 2 of pResponse)
end if
-Ken

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Performing a POST under iOS not returning a value?

Post by Mark » Thu Jan 19, 2012 5:58 pm

Hi Ken,

I tried to cause errors in several ways. The only way to get an error and not have LiveCode report it in iOS, was using the put command:

Code: Select all

put bla into url "http://humdiet.com/login.html"
This returns "error 405 Method Not Allowed" in the result in desktop environments, but nothing in iOS. This might be due to a lacking of put command in iOS' implementation of the http protocol. In all other cases, such as invalid host or 404 page not found, I always get a value returned in the result on both Mac and iOS. If no error occurs, the result is empty, as expected. If the result is empty, then you can assume an "error code" of 200. When LiveCode detects 200, it understands that no error ocurred and it returns empty in the result, which is standard behaviour for LiveCode (but I believe I have seen situations where the URL was returned in the result, so usually I check for something like "if the result is empty or the result contains "http" then...).

In your scripts and test results, I see nothing I wouldn't expect from LiveCode, except the empty result instead of a "method not allowed" error on iOS. BTW AFAIK the post command doesn't work at all on Android.

A few more helpful handlers would be socketError and socketTimeOut, but these haven't been implemented yet. I believe error 405 not being reported in iOS should be filed as a bug in the QCC. Everything else is just due to the incomplete implementation of the http protocol or simply because that's how things work in LiveCode.

Just one more thing about it and the result. It and the result and the way they work are essential elements of all xTalk languages. If you want to use LiveCode for your projects, you'll just have to cope with it. I am used to how they work and I wouldn't want them to change, but I agree that sometimes I would expect an error to be reported in the result while it isn't.

I can't think of a real solution. Usually, when an error occurs, I just do this:

Code: Select all

put bla into url "http://blaaaa.com"
put the result into rslt
put it into myData
if rslt is not empty then
  beep
  answer error rslt
else if it is somethingIDontExpect then //e.g. empty
  beep
  answer error "Sorry, an error occurred"
else
  // go ahead with script
end if
I hope this helps.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Performing a POST under iOS not returning a value?

Post by kcorey » Thu Jan 19, 2012 6:10 pm

After all your hard work on my silly question, I certainly owe you a beer. If we ever meet up, count on it.

Thanks for the answer. The code might not be as graceful as I might like, but I'll get used to it.

Trying to hit QCC now.

-Ken

Post Reply

Return to “iOS Deployment”