Skip to main content
March 21, 2023
Question

How to encode a text input for a URL?

  • March 21, 2023
  • 4 replies
  • 0 views

We are searching with a text (example: "xyz pqr") and this input is passed to the integration to call API but API call fails because of the space in the input text/string.
Expected outcome: want to encode the input like "xyz%20pqr".

Note: Input may have any special characters including or other than spaces.

    4 replies

    harshitb6843
    March 21, 2023

    Why not pass it in the query parameters directly instead of altering the URL on run time? 
    Query parameters are designed for this purpose only. 

    stewart.burchell
    March 21, 2023

    you're not saying exactly how you're using the text but on the assumption that you do need to parse the value and replace a set of special characters with some corresponding values here's an example of how you might go about it:

    a!localVariables(
      /* define a list of the special characters that needs replacing */
      local!specialChars: {
        " ",
        char(34),/* unicode for quotation mark */
        char(39) /* unicode for apostrophe */
      },
      /* define a list of the replacement characters - must be in the same order */
      /* as thelist of special characters */
      local!replaceWithChars: {
        "%20",
        """,
        "'"
      },
      /* use fn!reduce to pass the result of eahc iteration into the next iteration */
      fn!reduce(
        fn!substitute,/* use substitiue to replace the special characters */
        ri!myString,
        fn!merge( /* use fn!merge to create a "list of lists" */
          local!specialChars,
          local!replaceWithChars
        )
      )
    )

    Note:

    Line 12 should read """ but is obviously being parsed by this web-based app to read """

    Line 13 should read "'" for the same reason

    stefanhelzle0001
    Brainy
    March 21, 2023

    You can use the function urlwithparameters() to do this. In case you do not want to full URL, add some dummy values and remove them later on.

    Like so:

    mid(
      urlwithparameters(
        "/",
        {"x"},
        {ri!text}
      ),
      5,
      10000
    )

    mathieud0001
    Brainy
    March 21, 2023

    I use this approach as well.