How to encode a text input for a URL?

Certified Senior Developer

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.

  Discussion posts and replies are publicly visible

Parents
  • 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

Reply
  • 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

Children