Forum Discussion

David_Stout's avatar
David_Stout
Icon for Nimbostratus rankNimbostratus
Dec 17, 2014

String Manipulation and TCL Error

I'm up against a wall with my limited iRule knowledge so would like some input here.

We're taking some data within the original URI request and manipulating it into a new format so it's compatible with our environment.

Currently I'm getting this error

TCL error:  - can't read "UserID": no such variable while executing "set query_find "accountname=i%3A0%C7%B5%2Et%7Cadfs%20stage%7C${UserID}""

I'm using this in the iRule to transform the query string with the right most characters after the second | being the UserID. However it isn't working as in previous deployments.

set query_find "accountname=i%3A0%C7%B5%2Et%7Cadfs%20stage%7C${UserID}"
set query_replace "p%5Bid%5D=i%3A0%C7%B5.t%7Cadfs%7C${UserID}&p%5BidType%5D=adfs"

The decode for the query_find is accountname=i:0ǵ.t|adfs stage|${UserID}

How am I going to get ${UserID} to be the actual string in the URI and populate the variable correctly ?

Hopefully that makes sense. I'm still new on iRules.

2 Replies

  • You should be able to use this code instead. You need to parse out the UserId before you can use it. This is one way of doing that (as long as your parameter value is consistent in format). There's other methods of splitting a string to get values, but I think this will work.

     

    set param "accountname=i%3A0%C7%B5%2Et%7Cadfs%20stage%7CIAmAUserName"
    log local0. "Query: $param "
    
    set lastpipe [expr [string last "%7C" $param] "3"]
    log local0. "  Last pipe Index: $lastpipe"
    
    set UserId [string range $query $lastpipe end]
    log local0. "  Username: $UserId"
    
    set newparam "p%5Bid%5D=i%3A0%C7%B5.t%7Cadfs%7C${UserId}&p%5BidType%5D=adfs"
    log local0. "  New parameter value: $newparam"
    
     In 11.5+ you can do this
    set newquery [string map [list $param $newparam] [HTTP::query]]
    log local0. "  New Query: $newquery"
    HTTP::query $newquery
     Otherwise you can do this
    set newuri [string map [list $param $newparam] [HTTP::uri]]
    log local0. "  New URI: $newuri"
    HTTP::uri $newuri   
  • Thanks for the reply. I went back to the drawing board after looking at my own post and came up with this small change that seems to work just fine. Seeing as the query string is always of the same length I just applied the following simple fix.

    set query_find "accountname=i%3A0%C7%B5%2Et%7Cadfs%20stage%7C"
    set UserID [findstr [HTTP::uri] "|" 30 0]
    set query_replace "p%5Bid%5D=i%3A0%C7%B5.t%7Cadfs%7C${UserID}&p%5BidType%5D=adfs"
    

    Oh the fun of inheriting iRules written by someone else way before several software upgrades 🙂