Skip to main content
Participating Frequently
September 29, 2009
Question

URLEncodedFormat behavior

  • September 29, 2009
  • 1 reply
  • 1657 views

I have written a function which is located in a CFC.  The function takes two input parameters (the url stem, the URL structure).  For some reason I can't get the function to actually generate URL encoded URLs.  I have tested the URLEcondedFormat function in other functions and has no issues.  For this function I don't know why it doesn't seem to work.  Any ideas.

<cffunction name="constructUrl" returntype="string">
  <cfargument name="baseUrl" type="string" required="true" />
  <cfargument name="urlParams" type="struct" required="true" />

  <cfset var param = "" />
  <cfset var flag = "?" />
  <cfset var firstParam = "true" />

  <cfloop collection="#urlParams#" item="param">
    <cfif firstParam IS "false">
      <cfset flag = "&" />
    </cfif>
   
    <cfset baseUrl = baseUrl & flag & param & "=" & URLEncodedFormat(Trim(#urlParams[param]#)) />
    <cfset firstParam = "false" />
  </cfloop>

  <cfreturn baseUrl />
</cffunction>

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    September 29, 2009

    I would cut it like this:

    <!--- relevant: output="false" prevents Coldfusion adding space to output --->
    <cffunction name="constructUrl" returntype="string" output="false">
        <cfargument name="baseUrl" type="string" required="true" />
        <cfargument name="urlParams" type="struct" required="true" />
       
        <cfset var param = "" />
        <cfset var flag = "?" />
        <!--- scope the arg, and transfer value to local var --->
        <cfset var thisBaseUrl = arguments.baseUrl>
        <cfset var queryString = "">

        <cfif structCount(arguments.urlParams) GT 0>
            <cfloop collection="#urlParams#" item="param">
                <cfset queryString = queryString & flag & param & "=" & URLEncodedFormat(urlParams[param])>
                <cfset flag = "&" />
            </cfloop>   
            <cfset thisBaseUrl = thisBaseUrl & queryString>
        </cfif>

        <cfreturn thisBaseUrl />
    </cffunction>

    <cfset myDomain = "http://www.myDomain.com">
    <cfset myURLParams.username="BKBK">
    <cfset myURLParams.email="bkbk@myDomain.com">
    <cfset myURLParams.comment="Informative, diverse web site. Visitors' score: 9">
    <cfset myTestURL=constructUrl(myDomain,myURLParams)>

    <cfoutput>#myTestURL#</cfoutput>

    shness77Author
    Participating Frequently
    September 29, 2009

    I made the changes you mentioned by scoping the variables, but the end result is the same.  If I send in a URL parameter whose value is "a b c", the end result (function output) is a url whose parameter is still "a b c" rather than "a%20b%20c%20".  For some unknown reason to me the URLEncodedFormat function doesn't work in this situation.

    I have created other functions to test the URLEncodedFunction and they all worked as expected.  I am just not sure why the outputted URLs aren't encoded.

    This is the function ... I added the Trim function inside the URLEncodedFormat function.

    <cffunction name="constructUrl" returntype="string">
      <cfargument name="baseUrl" type="string" required="true" />
      <cfargument name="urlParams" type="struct" required="true" />

      <cfset var param = "" />
      <cfset var flag = "?" />
      <cfset var thisBaseUrl = arguments.baseUrl />
      <cfset var queryString = "" />

      <cfloop collection="#urlParams#" item="param">
        <cfset queryString = queryString & flag & param & "=" & URLEncodedFormat(Trim(urlParams[param])>
        <cfset flag = "&" />
      </cfloop>

      <cfset thisBaseUrl = thisBaseUrl & queryString />

      <cfreturn thisBaseUrl />
    </cffunction>

    Inspiring
    September 29, 2009

    I just copied your function into a test page and it worked for me. I did have to add a missing parenthesis at the end of the trim.

    This code:

    <cfset myStruct = structNew()>
    <cfset myStruct['foo'] = "a b c">
    <cfset myStruct['bar'] = "1 2 3">
    <cfset tmp = constructUrl('http://blah/',myStruct)>

    <cfoutput>#tmp#</cfoutput>

    Gave me this output:

    http://blah/?bar=1%202%203&foo=a%20b%20