Web Connection
Query string with multiple values
Gravatar is a globally recognized avatar based on your email address. Query string with multiple values
  Johnny
  All
  May 27, 2021 @ 10:56am

Hi,

I have a query string with multiple values https://example.com?field1=value1&field1=value2&field1=value3

Just wondering how to grab the values (of field1) from the Request object. I have tried Request.GetFormMultiple and Request.GetFormMultipleCollection with no success. It seems Request.Params returns only the first value.

Thanks.

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Rick Strahl
  Johnny
  May 27, 2021 @ 11:42am

Those are query string values not form values, so that's why GetFormMultiple() and GetFormMultipleCollection() aren't working. There's no support for retrieving multiple QueryString Values.

Where are those values coming from? If you're generating them from the a <form> use method="POST":

<form action="" method="post" class="form-horizontal" id="form1">

Well actually... here's a hack you can make this work even with Query String. You can cheat by assigning the QueryString to the Form buffer and then you can use the Form methods:

lcFirstField1 = Request.QueryString("field1")   && can only get first value

*** add query string values to Form buffer so we can use Form Methods
Request.cFormVars = "&" + Request.QueryString()

*** Now you can use Form values
loVars = loRequest.GetFormMultipleCollection("field1")

*** 3
lnCount = loVars.Count

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Rick Strahl
  Johnny
  May 27, 2021 @ 12:21pm

Ok, I'm adding a wwRequest method that handles this with a dedicated method:

************************************************************************
*  QueryStringMultipleCollection
****************************************
***  Function: Retrieves multiple query string values that have the
***            same key name as a collection.
***    Assume:
***      Pass: lcKey  -  The key with multiple values to retrieve
***    Return: Collection (0 items if no items)
************************************************************************
FUNCTION QueryStringMultipleCollection(lcKey)
LOCAL loValues, lcPoint, lcFind, lnAt, lcValue, lnX
loValues = CREATEOBJECT("Collection")

lcPointer = "&" + THIS.cQueryString
lcFind = "&"+STRTRAN(lcKey," ","+")+"="
lnAt = ATC(lcFind,lcPointer)
IF lnAt = 0
   RETURN 0
ENDIF

DO WHILE lnAt > 0
	lcValue = GetUrlEncodedKey(lcPointer,lcKey) 
	loValues.Add(lcValue)

	lcPointer = SUBSTR(lcPointer,lnAt + LEN(lcFind))
	lnAt = ATC(lcFind,lcPointer)
ENDDO 

RETURN loValues 
ENDFUNC
* QueryStringMultipleCollection

Here's the documentation (for next update):

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Johnny
  Rick Strahl
  May 28, 2021 @ 01:56am

It worked. Thanks so much.

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Johnny
  Johnny
  May 28, 2021 @ 02:26am

Hi Rick, A quick follow up question. The function returns a **Collection ** object. Is it possible to get the list of items like **ToArray ** method on the **wwCollection ** object or do I have to loop through to get values i.e. any helper method to get the values as an array?

Thanks

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Rick Strahl
  Johnny
  May 28, 2021 @ 09:53am

Uh, you have to loop through an Array too, no? What is it that you need about an array that you can't get a from collection?

No there won't be an array version - the GetFormMultiple() array version is really there only because it was the original legacy. I wouldn't return arrays for anything these days.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Johnny
  Rick Strahl
  May 31, 2021 @ 06:17am

I was calling a library that required a string array. Have got it working with the Collection object. Thanks

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Rick Strahl
  Johnny
  Jun 1, 2021 @ 06:24pm

FWIW, I added a CollectionToArray() function to wwutils that will automate that - I thought that was already there, but turned out it wasn't.

Then again manual looping isn't too complicated 😄 You can add this to your wwutils.prg if you like until the next update provides it:

************************************************************************
*  CollectionToArray
****************************************
***  Function: Turns a collection into a 1 dimensional array
***    Assume:
***      Pass: @laArray       - an array var that will be created
***            loCollection  - a collection to read from
***    Return: count of records
************************************************************************
FUNCTION CollectionToArray(laArray, loCollection)
LOCAL lnCount, lnX

IF VARTYPE(loCollection) != "O"
   DIMENSION laArray[1]  && empty array
   RETURN 0
ENDIF

lnCount = loCollection.Count
DIMENSION laArray[lnCount]
FOR lnX = 1 TO lnCount
    laArray[lnX] = loCollection[lnX]
ENDFOR

RETURN lnCount
ENDFUNC
*   CollectionToArray

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Query string with multiple values
  Johnny
  Rick Strahl
  Jun 2, 2021 @ 02:24am

Greatly appreciate the effort. Thanks.

© 1996-2021