Search code examples
coldfusioncoldfusion-11luceetaffy

a query string into a structure


I am using taffy and am passing an unknown query string to a function. I do not know the query string values passed in advance, so I am trying to use that in the function but it is not working. Please point me to right direction.

Here is my code:

<cffunction name="qrystringToStruct" returntype="any">
    <cfargument name="myStruct" default="#structNew()#" type="struct">
    <cfargument name="str" required="true" default="">
    <cfscript>
        for(i=1; i LTE listLen(arguments.str,'&');i=i+1) {
        structInsert(myStruct, i, listGetAt(arguments.str,i,'&'));
        }
    </cfscript>
    <cfreturn myStruct>
</cffunction>

<cffunction name="getCourseById" taffy:verb="get" taffy:docs:hide>
  <cfargument name="structurl" type="any" default="" />
  <cfdump var="#structurl#">
 <cfdump var="#qrystringToStruct(structurl)#" abort> 
  <cfset var local = {} />

This is how I am calling the url:

http://localhost:9002/taffy/index.cfm//coursesMethods?credits=3&coursetitle=power 

but all I am getting is [empty string]


Solution

  • Let me just preface this by saying I've never used Taffy. However with that said, I don't think it's relevant to the problem specified in your posted question. There are a few things in your code that's puzzling to me.

    • Your call qrystringToStruct(structurl) passes one parameter but your function definition has two parameters.
    • Why would you declare myStruct as a parameter and then <cfreturn myStruct> in your qrystringToStruct function definition? It makes no sense.
    • You say you pass the full url to http://localhost:9002/taffy/index.cfm//coursesMethods?credits=3&coursetitle=power? Why not just pass the querystring portion using cgi.QUERY_STRING?

    Anyhow, I think you're overcomplicating this and you don't need a custom function to parse out your querystring. All you need is one line of code.

    <cfset qryString = listToArray(cgi.QUERY_STRING, "&")>
    

    You can test it out here here.