2

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]

Community
  • 1
  • 1
JJK
  • 77
  • 4
  • 4
    Silly question. What is wrong with the `url.*` struct? It automatically changes the http query string into a struct. – James A Mohler Feb 11 '18 at 05:01
  • @JamesAMohler good question. However, from the way his question is written, it sound like he doesn't know or can't predict the url structure keys. – user9263373 Feb 11 '18 at 05:18

1 Answers1

3

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.

user9263373
  • 1,054
  • 8
  • 15
  • Hi, Ok Thanks but i think i missed the query which i had written to Loop over the the structure in a query command, that was liettrally my mitake while posting the question, i missed it. Here is the query I am trying to do: ` select * from course where 1=1 OR #kk# = "#trim(arguments.structurl['k'])# ` – JJK Feb 11 '18 at 15:38
  • I changed the function to this: ` ` and i had the fiddle here for the one you created: https://trycf.com/gist/f745d2f9258b392ad64a16b369ffe954/acf11?theme=monokai – JJK Feb 11 '18 at 15:43
  • 1
    @JJK Ok, after comparing what you were trying to do in your original post vs your updated gist, now I can see what you're doing. Again, you are overcomplicating things. You're recreating ColdFusion built-in functionality. What you need is already in the `url` scope as originally mentioned by James Mohler. Try this to verify ``. No need for a custom function. – user9263373 Feb 11 '18 at 16:04
  • 2
    @JJK - Please edit your question and *append* the current code, so it's clear what you're actually trying to do. That said 1) James and user9263373 are probably correct that you don't need to create a structure of parameters - you already have one: the `URL` scope. 2) **Never** use raw client values in a SQL query. It's a sql injection risk. 3) Functions should always `local` scope function variables, including loop indexes, to avoid threading problems. – SOS Feb 12 '18 at 14:50