0

I'm receiving post data on the server-side which resembles a querystring:

type=subscribe&fired_at=2021-07-16 07:51:09&data[id]=ebdc32dce6&data[email]=xyz@fakeemail.com&data[email_type]=html&data[ip_opt]=110.20.71.126&data[ip_signup]=110.100.22.217&data[web_id]=566760869&data[merges][EMAIL]=xyz@fakeemail.com.com&data[merges][FNAME]=&data[merges][LNAME]=&data[merges][ADDRESS]=&data[merges][PHONE]=&data[merges][BIRTHDAY]=&data[list_id]=e971d14

The parameters are unknown so I'm looking for a dynamic solution. Note, the "data" parameter is actually an array of sorts.

I'd love to be able to retrieve key fields like:

Dim id as String = ParamList( "id" )
Dim email as String = ParamList( "email" )

What's the best way to achieve this?

Sami.C
  • 561
  • 1
  • 11
  • 24
  • Have you seen [Easiest way to parse “querystring” formatted data](https://stackoverflow.com/questions/11956948/easiest-way-to-parse-querystring-formatted-data)? It looks like some further parsing of the keys will be needed for the example in the question. – Andrew Morton Jul 18 '21 at 10:36

1 Answers1

1

As Andrew has indicated:

Dim s = "type=subscribe&fired_at=2021-07-16 07:51:09&data[id]=ebdc32dce6&data[email]=xyz@fakeemail.com&data[email_type]=html&data[ip_opt]=110.20.71.126&data[ip_signup]=110.100.22.217&data[web_id]=566760869&data[merges][EMAIL]=xyz@fakeemail.com.com&data[merges][FNAME]=&data[merges][LNAME]=&data[merges][ADDRESS]=&data[merges][PHONE]=&data[merges][BIRTHDAY]=&data[list_id]=e971d14"
Dim c = HttpUtility.ParseQueryString(s); 'need to Imports System.Web

Note that the parser doesn't care about the data[...] as being "a dictionary" etc - you say you just want to write the ... but the whole key includes the data[ and ] too.. This means you either :

Dim id = c( "data[id]" )
Dim email = c( "data[email]" )

Or you could generate a new collection, such as a dictionary, with altered keys:

Dim d = c.Keys.Cast<string>().ToDictionary( _
    Function(k) k.Split("[]".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Last(), _
    Function(k) c(k) _
)

This can be indexed like

Dim id = d("id")
Dim bd = d("BIRTHDAY")

But be careful with ToDictionary: the keys need to be unique. If they aren't, you cna use ToLookup but note that the entries in the "dictionary" will then be enumerables (lists of values) not single values

Caius Jard
  • 72,509
  • 5
  • 49
  • 80