nakb
nakb

Reputation: 341

Generate query string from string[]

In C# I'm using UriBuilder to build a GET request. Works fine except for where the query string needs to represent a string array using square brackets notation. How do I do that?

The data to form the query is in the form: string[] obs_section

The query string needs to look like (e.g.): 'obs_section':['a','b','c']

I tried:

Query["obs_section"] = JsonConvert.SerializeObject(obs_section)

but that gives the wrong format.

Here is what a working example query in Python looks like. I'm trying to replicate in C#:

entry = requests.get("https://filtergraph.com/aavso/api/v1/targets",auth=(userid,password),params={'obs_section':['a','b','c']})

Upvotes: 2

Views: 87

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

You can use string.Join() to join an array and make a string:

string QS = $"'obs_section':[{string.Join(",",obs_section.Select(x => $"'{x}'"))}]";

Here is a Live Demo of how it works.

Upvotes: 1

Related Questions