Forum Discussion

Christopher_J_B's avatar
Christopher_J_B
Icon for Nimbostratus rankNimbostratus
Sep 22, 2010

Attempting to remove newline/spaces from a HTTP:payload without sucess

I have an irule that parses http payload that contain xml using the findstr function; the problem is some vendors xml requests have spaces and/or newline characters which getting difficult to account for. I am attempting to perform a string map - replacing spaces and newline chars with empty space. I keep getting errors and I cannot seem to get to save without errors.

 

 

Here is the actual line - attempting to replace all newline & spaces with empty from the HTTP:payload - while capturing string into the variable

 

 

set tmp [findstr [string map [" " "" "\n" ""][HTTP::payload]] "<" 16 ":"]

 

 

Here are the errors for the above line

 

line 32: [wrong args] [string map [" " "" "\n" ""][HTTP::payload]]

 

line 32: [undefined procedure: ] [" " "" "\n" ""]

 

 

 

I tried this variation - no joy

 

set tmp [findstr [string map [list " " "" "\n" ""][HTTP::payload]] "<" 16 ":"]

 

 

Got this error

 

line 32: [wrong args] [string map [list " " "" "\n" ""][HTTP::payload]]

 

 

Any suggestions?

 

9 Replies

  • Hi Christopher,

     

     

    Which LTM version are you testing this with?

     

     

    The second string map syntax looks correct and works with tclsh:

     

     

    % string map [list " " "" "\n" ""] "text with spaces followed by \

     

    a new line"

     

    textwithspacesfollowedbyanewline

     

     

    I don't have access to a test box at the moment to try this with findstr. Can you separate the findstr and string map commands with another temporary variable and check whether it is actually the string map or findstr commands generating the syntax error?

     

     

    If the issue is with the string map command, you could try an alternate syntax:

     

     

    % string map [list { } {} \n {}] "string with spaces and \

     

    new line

     

    s...

     

    "

     

    stringwithspacesandnewlines...

     

     

    Aaron
  • Hi Chris!

     

     

    Using brackets should work fine, but sometimes the BigIP's validation routines are a little... uh... anal.

     

     

    set tmp [findstr [string map [" " "" "\n" ""][HTTP::payload]] "<" 16 ":"]

     

     

    might be best converted to:

     

     

    set tmp [findstr [string map {" " "" "\n" ""} [HTTP::payload]] "<" 16 ":"]

     

     

    The bottom one applies correctly on 9.x and 10.x, the top one complains about the undefined procedure (the parser -- incorrectly -- thinks that the bracketed syntax should contain some sort of command). In testing this, it appears to work fine, although smacking around a payload in this way is a bit scary to me...

     

     

  • I think wrapping the find/replace token in square braces won't work as the square brackets act like backticks in *nix and mean the string within is executed as a command.

     

     

    Aaron
  • Hoolio, that's true enough. It's interesting, though, that tclsh will let you do it without issues -- I guess converting the brackets to act like curly-braces if there's no actual command/procedure to execute inside it? Not that it's the BigIP parser's fault, really... just an interesting difference in behavior between it and tclsh.

     

     

    It's academic, really, as the official TCL documentation for the "string map" command clearly uses curly-braces for search items.
  • I think the use of square braces is the same in TCL and iRules. With tclsh, this doesn't work:

     

     

    % string map [" " "" "\n" ""] "test string with spaces"

     

    invalid command name " "

     

     

    % info patchlevel

     

    8.4.1

     

     

    Curly braces will work though:

     

     

    % string map {" " "" "\n" ""} "test string with spaces"

     

    teststringwithspaces

     

     

    Or have I missed what you're saying?

     

     

    Aaron
  • I have a window tonight - I will test with the curly braces {} - thanks for the help

     

  • Thanks for the assist - this was correct ->

     

    set tmp [findstr [string map {" " "" "\n" ""} [HTTP::payload]] "<" 16 ":"]

     

     

    Unforuntalty didnt solve my problem, but this does help - thank you.

     

     

    Ahh I cannot wait when JSON is favored over XML