Forum Discussion

dbulli's avatar
dbulli
Visitor
12 years ago

PostFromString Array??

Is there anyway to post an array to server?


events[0][ev]:event1
events[0][ts]:1374269099
events[0][pos]:0
events[1][ev]:event2
events[1][ts]:1374269100
events[1][pos]:0
events[2][ev]:event3
events[2][ts]:1374269100
events[2][pos]:0

5 Replies

  • Well, you can post an AA (associative array) to a server by treating it as an array of headers using roURLTransfer.setheaders(AA).

    Or you could convert the whole thing to a string of name=value&name=value&name=value pairs and post that.

    Not sure if you intend to post each element in events at once, or each AA within events separately, in which case the first approach could work.

    - Joel
  • Thanks Joel, the back end server is php and when I print $_POST i don't see anything being passed. The backend would have to be rewritten to look on the HEADERS.

    Every time I have tried to encode the string, then I get an error? I would like to iterate over the events on the server side like this:

    foreach($_POST['events'] as $event){
    echo $event . ' ';
    }
  • You have to serialize the data somehow, either to the querystring or to the post body (url encoded name/value pairs, json, or xml). How would you submit the data via a website or another app? The same would be true in BrightScript.
  • Solved like this:


    postString = ""
    ander =""
    pos% = 0
    for each event in events
    eventString = "events["+AnyToString(pos%)+"][ev]="+AnyToString(event["ev"])
    eventString = eventString+"&events["+AnyToString(pos%)+"][ts]="+AnyToString(event["ts"])
    eventString = eventString+"&events["+AnyToString(pos%)+"][pos]="+AnyToString(event["pos"])
    postString = postString + ander + eventString
    ander = "&"
    pos% = pos% + 1
    end for

    xfer.PostFromString(postString)

    '******************************************************
    'HELPERS
    '******************************************************


    '******************************************************
    'itostr
    '
    'Convert int to string. This is necessary because
    'the builtin Stri(x) prepends whitespace
    '******************************************************
    Function itostr(i As Integer) As String
    str = Stri(i)
    return strTrim(str)
    End Function

    '******************************************************
    'Trim a string
    '******************************************************
    Function strTrim(str As String) As String
    st=CreateObject("roString")
    st.SetString(str)
    return st.Trim()
    End Function

    'try to convert an object to a string. return invalid if can't
    '******************************************************
    Function AnyToString(any As Dynamic) As dynamic
    if any = invalid return "invalid"
    if isstr(any) return strTrim(any)
    if isint(any) return itostr(any)
    if isbool(any)
    if any = true return "true"
    return "false"
    endif
    if isfloat(any) return Str(any)
    if type(any) = "roTimespan" return itostr(any.TotalMilliseconds()) + "ms"
    return invalid
    End Function
  • "TheEndless" wrote:
    You have to serialize the data somehow, either to the querystring or to the post body (url encoded name/value pairs, json, or xml). How would you submit the data via a website or another app? The same would be true in BrightScript.


    Endless, solved but HTML forms handle this automatically. THanks for taking a look.