Posting data to LiveCode Server

Are you using LiveCode to create server scripts or CGIs?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
dcpbarrington
Posts: 87
Joined: Tue Nov 13, 2007 6:40 pm

Posting data to LiveCode Server

Post by dcpbarrington » Fri Nov 30, 2012 10:35 pm

Forum,

I'm working on sending data from a livecode (5.5.3) Windows app to a central web server that is running LiveCode Server. I'm just putting the solution together and for some reason I cannot get the server to extract any of the data the I'm sending up in the POST message. I've even run the server script as part of the app before sending it to the server and the local app is able to extract the values, but the server is not.

Here is the LiveCode app code that is placed in a button an a card in a Windows environment.

Code: Select all

on mouseUp
   global gACTsaverLocation
   local tActRequest, tBodySize, tRequestBody, tRequestHeader
      
    
   -- Build the NWAC Request Header
   put createActRequestHeader( "NWAC", "NO", "100") into tRequestHeader
   put tRequestHeader into tActRequest
   put URLEncode(tActRequest) into tActRequest
   
   -- Send the message to the ACTsaver Server
   set the httpHeaders to "Content-Type: application/x-www-form-urlencoded"   --  ???????
   post tActRequest to url "<server address>ACTsaver_server.lc"
   put it into myResult
   put URLDecode(myResult) into myResult
   
   -- can't connect to DB, code "*" sent byserver
   if myResult="*" then
      answer "DB error"
      exit to top
   end if
   answer myResult
end mouseUp

function createActRequestHeader tMsgType tMsgEncrypt tMsgSize
   local tActRequestHeader
   
   put "SAVER_Server=1.0"& "&" into tActRequestHeader
   -- Get the Date and Time
   put the seconds into tDateTime
   convert tDateTime to dateItems
   delete item 7 of tDateTime
   replace "," with "-" in tDateTime
   put "Date=" & tDateTime & "&" after tActRequestHeader
   put "Type="& tMsgType & "&" after tActRequestHeader
   put "Size=" & tMsgSize & "&" after tActRequestHeader
   if tMsgEncrypt is YES then
      put "Encrypt=SCR1" & "&" after tActRequestHeader
   else
      put "Encrypt=NONE" & "&" after tActRequestHeader
   end if
   put "Segmented=NO" & "&" after tActRequestHeader
   put "form_submitted=true" after tActRequestHeader
   put RETURN after tActRequestHeader
   
   return tActRequestHeader
   
end createActRequestHeader
In the server I have created a simple stack that just takes the values out the variables and then returns them to the calling app.
I initially tried to use the $_POST array, but could not get any data from the variables, so I'm using more of a brute force method.
The server is on a Windows Workstation with a WAMP server installed. LiveCode Server V5.0.2 for Windows.

Code: Select all

<?lc set the errorMode to "inline" ?>
<h1>Test Post</h1>
<?lc
local tRequestType, tRequestSize, tRequestDate, tRequestVersion, tRequestEncrypt, tRequestSegmented
local tRequest
local tResponseMsg

put $_POST_RAW into tRequestHeader
## Get the values out of the message
set the itemdelimiter to "&"
repeat for each item tVariablePair in tRequestHeader
   set the itemdelimiter to "="
   put urlDecode(item 1 of tVariablePair) into tVariableKey
   put urlDecode(item 2 of tVariablePair) into tVariableValue
   switch tVariableKey
	case "Type"
		put tVariableValue into tRequestType
	break
	case "Size"
		put tVariableValue into tRequestSize
	break
	case "Date"
		put tVariableValue into tRequestDate
	break
	case "SAVER_Version"
		put tVariableValue into tRequestVersion
	break
	case "Encrypt"
		put tVariableValue into tRequestEncrypt
	break
	case "Sequenced"
		put tVariableValue into tRequestSequenced
	break
	default
	break
   end switch
   set the itemdelimiter to "&"
end repeat

put "Req Type = "& tRequestType & "Req Size = " & tRequestSize" into tResponseMsg
## put URLEncode(tResponseMsg) into tResponseMsg
put tRequestHeader after tResponseMsg
put tResponseMsg
?>

What is returned to the APP is the following.

Test Post
Req Type = Req Size =
SAVER_Server=1.0&Date=2012-11-30-15-24-57&Type=NWAC&Size=100&Encrypt=NONE&Segmented=NO&form_submitted=true


So the $_POST_RAW value is returned, but the Type and Size have not been extracted from the message.
I must be missing something basic in how the server is using variables and processing data. This should work

I've also used the libURLFormData to pack the message in the app and get exactly the same results.

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Location: Thailand
Contact:

Re: Posting data to LiveCode Server

Post by dave_probertGA6e24 » Sat Dec 01, 2012 9:04 am

Hi,

I think it's just a typo on your part...

You have an extra quote in the line (last one on the line):
put "Req Type = "& tRequestType & "Req Size = " & tRequestSize" into tResponseMsg

whereas this works perfectly on my copy of your code:

put "Req Type = "& tRequestType & " Req Size = " & tRequestSize &&"....."&cr into tResponseMsg

Returns:
Req Type = NWAC Req Size = 100 .....
SAVER_Server=1.0&Date=2012-11-30-15-24-57&Type=NWAC&Size=100&Encrypt=NONE&Segmented=NO&form_submitted=true

Using the dots as a delimiter shows that the raw data is not in the results - only what is tacked on at the end.

Hope that helps.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

dcpbarrington
Posts: 87
Joined: Tue Nov 13, 2007 6:40 pm

Re: Posting data to LiveCode Server

Post by dcpbarrington » Sat Dec 01, 2012 11:06 am

Thanks,

I removed the bad quote in the PUT statement, however on my configuration of Windows LC Server I still get the parameters returned as blank.

Test Post
Req Type = Req Size =
SAVER_Server=1.0&Date=2012-11-30-15-24-57&Type=NWAC&Size=100&Encrypt=NONE&Segmented=NO&form_submitted=true


Is there some type of configuration that I'm missing on my LC Server? I've installed the LC Server in the CGI-BIN folder for Apache. Do I need to configure the GATEWAY_INTERFACE variable for it to work properly? I assumed that would be set automatically when the server is running in the CGI mode. The code is so simple and it works if I put it in a function on my LC Windows Stack.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Posting data to LiveCode Server

Post by sturgis » Sat Dec 01, 2012 4:24 pm

As long as you are developing for desktop only you might switch to using liburlformdata (in the dictionary) instead.

It lets you do something like this..

put libUrlFormData("keyname",tKeyValue,"nextKeyName",tNextKeyValue) into myValuetoPost

post myValueToPost to "wherever"

It does the proper url encoding of the strings but doesn't mess with any & or = in the result.

If you need to stay with the method you are currently using you need to urlencode each value string and THEN build your post string. Otherwise you end up with 1 long string being sent, and everything can go weird on you.

So the modified code NOT using liburlformdata follows:

Code: Select all

global gACTsaverLocation
on mouseUp
   local tActRequest, tBodySize, tRequestBody, tRequestHeader
      
    
   -- Build the NWAC Request Header
   put createActRequestHeader( "NWAC", "NO", "100") into tRequestHeader
   put tRequestHeader into tActRequest
   --put URLEncode(tActRequest) into tActRequest -- don't encode the whole tActRequest, just the individual strings
   -- Send the message to the ACTsaver Server
   set the httpHeaders to "Content-Type: application/x-www-form-urlencoded"   --  ???????
   post tActRequest to url "http://my.server.somewhere/test.lc"
   put it into myResult 
   put myResult
   put URLDecode(myResult) into myResult
   
   -- can't connect to DB, code "*" sent byserver
   if myResult is "*" then
      answer "DB error"
      exit to top
   end if
   --answer myResult
end mouseUp

function createActRequestHeader tMsgType tMsgEncrypt tMsgSize
   local tActRequestHeader
   
   put ("SAVER_Server=1.0")& "&" into tActRequestHeader
   -- Get the Date and Time
   put the seconds into tDateTime
   convert tDateTime to dateItems
   delete item 7 of tDateTime
   replace "," with "-" in tDateTime
   put "Date=" & urlencode(tDateTime) & "&" after tActRequestHeader
   put "Type="& urlEncode(tMsgType) & "&" after tActRequestHeader
   put "Size=" & urlencode(tMsgSize) & "&" after tActRequestHeader
   if tMsgEncrypt is YES then
      put "Encrypt=SCR1" & "&" after tActRequestHeader
   else
      put "Encrypt=NONE" & "&" after tActRequestHeader
   end if
   put "Segmented=NO" & "&" after tActRequestHeader
   put "form_submitted=true" after tActRequestHeader
   put RETURN after tActRequestHeader
   
   return tActRequestHeader 
end createActRequestHeader
EDIT: Also noticed you are doubling up on the return of some info, and as well the format varies due to how the system works.

This line: put "Req Type = "& tRequestType & "Req Size = " & tRequestSize into tResponseMsg

Will cause output like: Req Type = NWACReqSize = 100SAVER_Server=1.0&.......
Plus, since you are returning all the posted data you are returning type, size and version twice.

If you wish to return it twice you can do so, but you'll want to format the above line differently to avoid things running together.
put "Req Type = "& tRequestType & "&" & "Req Size = " & tRequestSize & "&" into tResponseMsg

It might be easier to use merge for this.

put merge("Req type = [[tRequestType]] & Req Size = [[tRequestSize]] & ") into tResponseMsg -- all [[ ]] will be evaluated

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Posting data to LiveCode Server

Post by sturgis » Sat Dec 01, 2012 5:51 pm

Just thought of another thing you might consider. Rather than use $_POST_RAW you could just use $_POST and a repeat loop.

Code: Select all

repeat for each key tKey in $_POST
put "tKey = " & $_POST[tkey] & "&" after tPostDat 
end repeat
delete the last char of tPostDat -- remove the trailing &
put tPostDat -- will now contain a list of keys and values that were recieved with post
This way you can more easily see how your pairs and values are matching up so that you can find syntax/quoting errors more easily.

dcpbarrington
Posts: 87
Joined: Tue Nov 13, 2007 6:40 pm

Re: Posting data to LiveCode Server

Post by dcpbarrington » Mon Dec 03, 2012 4:49 pm

Thank you so much for the help.

Encoding each individual item before sending them did the trick. Encoding the entire message string was the problem.

Now the basics work, now I can get on to the more interesting features.

Post Reply

Return to “CGIs and the Server”