scottm scottm - 5 months ago 42
PHP Question

How can I send an array as a parameter of a query string in c#?

I am trying to use a home grown web API to retrieve some data. The documentation is all written in PHP. The example I'm looking at is this:

$params = array(
'id' => 1
,'data' => array(
,'email' => 'example@hasoffers.com'
)

$url = "www.someapi.com/api?" . http_build_query( $params );


I'm using the C# WebClient class, but I can't figure out how to serialize the data parameter:

WebClient wc = new WebClient();
wc.QueryString["id"] = "1";
wc.QueryString["data"] = // I have no idea.

string json = wc.DownloadString(apiUrl);


I've tried a few variations:

wc.QueryString["data"] = "email=test@stackoverflow.com";
wc.QueryString["data"] = Uri.EscapeDataString("data[email]=test@stackoverflow.com");
wc.QueryString["data"] = Uri.EscapeDataString("email[0]=test@stackoverflow.com");
wc.QueryString["data"] = Uri.EscapeDataString("email=test@stackoverflow.com");


Of course, I don't have PHP setup anywhere to see what the http_build_query() is actually returning.

Answer

I finally figured it out. I guess posting this question rebooted my brain.

This is what worked:

wc.QueryString["data[email]"] = "test@stackoverflow.com";