0
votes

I have a lotus agent running with lotusscript. Form the browser I post form data to the webserver and I receive this data with the following lotusscript:request_method = doc.GetItemValue( "request_content" )(0)

But if I have a form with for example name and phonenumber. Then my agent receives this as name=bla&phonenumber=243525

How can i separate these fields actually and secondly how can I receive XML on this agent so that I can extract this and put in to a document. I googled a lot but still got no solutions.

3

3 Answers

2
votes

The way you get the data differs if the client makes a GET or a POST. If this is a get, all the parameters are in the url in a url format.

Many many ressource on the web will give you some code to parse this url and get name and value a simple search in goolge will bring : http://searchdomino.techtarget.com/tip/Parsing-URL-Parameters-with-Ease

I use generally the following code, which add in the document context the fields received on url or on post.

    Dim s As NotesSession
Set s = New notessession
Set doc = s.documentcontext
Dim myQuerystring As String

If doc Is Nothing Then
    logErrorEX "getting a call without document context ?!?","", doc,""
    GoTo returnErr
End If
If doc.QUERY_STRING_DECODED(0)<>"" Then'it's a GET
    myQuerystring = doc.QUERY_STRING_DECODED(0)
ElseIf doc.QUERY_STRING(0)<>"" Then
    myQuerystring = doc.QUERY_STRING(0)
    'decode it !
ElseIf doc.REQUEST_CONTENT(0)<>"" Then'it's a POST
    myQuerystring = doc.REQUEST_CONTENT(0) ' WARNING this is for POST but you will have to decode !!!
    'decode it !
Else
    logErrorEX "getting a call with document context but without query_string?!?","", doc,""
    GoTo returnErr
End if
Call ExplodeQueryString(myQuerystring, doc)

Private Sub  ExplodeQueryString (QueryString As String,doc As NotesDocument )

Dim ArgsList As Variant 

ArgsList = Split (QueryString,  "&")
If IsArray(ArgsList) Then 
    debugString = debugString+"ArgsList is an array of " & UBound(ArgsList) 
Else
    debugString = debugString+"ArgsList is NOT an array ??? " & ArgsList
End if
Dim ArgKey As String
Dim ArgValue As String
ForAll Arg In ArgsList
    If left$(Arg, 1)= "_" Or Left$(Arg, 1)= "%" Then
        'ignore it      
    else 
        ArgKey = strleft(Arg, "=")
        If ArgKey = "" Then
            'ignore it?
        else
            ArgValue = strright$(Arg, "=")
            '               AgentArgs(ArgKey) = ArgValue
            doc.Replaceitemvalue ArgKey, ArgValue
        End If
    End if
End ForAll
End Sub

I didn't declare some global variable like debugString to shorten in.

0
votes

The format you are seeing is the convention used by all web browser software to encode field data from forms. You can use functions similar to the ExplodeQueryString function in the code posted by Emmanual to parse it. It looks to me like he is taking each "&name" portion and creating a NotesItem with that name and using it to store the value from the "=value" portion. You can do that, or you can use a List, or whatever best fits your requirements.

There is no rule against sending POST data in other formats without using the &name=value convention. It just requires agreement between whatever software is doing the sending and your software on the receiving side. If they want to send you XML in the POST data, that's fine. You can use standard XML parsing functions to deal with it. Notes comes with a NotesDOMParsesr class that you can use if you want. If you are running on Windows, you can use Microsoft.XMLDOM instead.

0
votes

I wrote a class a while back that does exactly what you ask for. It splits the query string (or request content) into a list of values, with the name as the list tag.

http://blog.texasswede.com/free-code-class-to-read-url-name-value-pairs/

Here is the code (I usually put it in a script library called Class.URL):

%REM
    Library Class.URL
    Created Oct 9, 2014 by Karl-Henry Martinsson
    Description: Lotusscript class to handle incoming URL (GET/POST).
%END REM
Option Public
Option Declare

%REM
    Class URLData
    Description: Class to handle URL data passed to web agent
%END REM
Class URLData
    p_urldata List As String

    %REM
        Sub New()
        Description: Create new instance of URL object from NotesDocument 
    %END REM
    Public Sub New()
        Dim session As New NotesSession
        Dim webform As NotesDocument
        Dim tmp As String
        Dim tmparr As Variant  
        Dim tmparg As Variant
        Dim i As Integer

        '*** Get document context (in-memory NotesDocument)
        Set webform = session.DocumentContext
        '*** Get HTTP GET argument(s) after ?OpenAgent
        tmp = FullTrim(StrRight(webform.GetItemValue("Query_String")(0),"&"))
        If tmp = "" Then
            '*** Get HTTP POST argument(s) after ?OpenAgent
            tmp = FullTrim(StrRight(webform.GetItemValue("Request_Content")(0),"&"))    
        End If
        '*** Separate name-value pairs from each other into array
        tmparr = Split(tmp,"&")      
        '*** Loop through array, split each name-value/argument 
        For i = LBound(tmparr) To UBound(tmparr)
            tmparg = Split(tmparr(i),"=")
            p_urldata(LCase(tmparg(0))) = Decode(tmparg(1))
        Next
    End Sub

    %REM
        Function GetValue
        Description: Get value for specified argument.
        Returns a string containing the value.
    %END REM
    Public Function GetValue(argname As String) As String
        If IsElement(p_urldata(LCase(argname))) Then
            GetValue = p_urldata(LCase(argname))
        Else        
            GetValue = ""   
        End If
    End Function

    %REM
        Function IsValue
        Description: Check if specified argument was passed in URL or not.
        Returns boolean value (True or False).
    %END REM
    Public Function IsValue(argname As String) As Boolean
        If IsElement(p_urldata(LCase(argname))) Then
            IsValue = True
        Else        
            IsValue = False 
        End If
    End Function


    '*** Private function for this class
    '*** There is no good/complete URL decode function in Lotusscript
    Private Function Decode(txt As String) As String
        Dim tmp As Variant 
        Dim tmptxt As String
        tmptxt = Replace(txt,"+"," ")
        tmp = Evaluate(|@URLDecode("Domino";"| & tmptxt & |")|)
        Decode = tmp(0)
    End Function

End Class

And this is how you can use it:

Option Public
Option Declare
Use "Class.URL"

Sub Initialize
    Dim url As URLData

    '*** Create new URLData object
    Set url = New URLData()

    '*** MIME Header to tell browser what kind of data we will return
    Print "content-type: text/html"

    '*** Check reqired values for this agent
    If url.IsValue("name")=False Then
        Print "Missing argument 'name'."
        Exit Sub
    End If

    '*** Process name argument
    If url.GetValue("name")="" Then
        Print "'Name' is empty."
    Else
        Print "Hello, " + url.GetValue("name") + "!"
    End If

End Sub