Is it possible to insert arrays into HTTP request URIs using C#?

Forums C#Is it possible to insert arrays into HTTP request URIs using C#?
Staff asked 11 months ago

Answers (1)

Add Answer
Umang Ramani Marked As Accepted
Staff answered 11 months ago

Yes, it is possible to insert arrays into HTTP request URIs using C#.

One way to do this is to convert the array into a query string and append it to the URI. The query string is a part of the URI that contains key-value pairs separated by an equals sign (=) and the pairs are separated by an ampersand (&) character. Here is an example of how to construct a URI with an array as a query string using C#:

int[] numbers = { 1, 2, 3, 4, 5 };
string queryString = string.Join("&", numbers.Select(x => "numbers=" + x.ToString()));
string uri = "http://example.com/api?"+queryString;

 

In this example, the numbers array is converted to a query string by using the Select method to iterate over each element of the array and generate a string representation of it in the format “numbers=x”, where x is the element’s value. The resulting array of strings is then joined into a single string with the ampersand character separating the key-value pairs. Finally, the query string is appended to the base URI using the question mark (?) character.

This will result in a URI that looks like this:

http://example.com/api?numbers=1&numbers=2&numbers=3&numbers=4&numbers=5

 

Note that the query string can be customized to fit the needs of your specific API endpoint.

Subscribe

Select Categories