How to use c# to call Spark API

Can anyone help me out with a sample API call for the Control two LEDS from the internet sample app. I want to use c# to make my api calls but can not find a good translation for the curl sample to c#… Anyone?? Thanks in advance.

Hi @themaddgo1068,

Ah, I don’t have my trusty copy of Visual Studio in front of me, but as usual StackOverflow delivers: http://stackoverflow.com/questions/4088625/net-simplest-way-to-send-post-with-data-and-read-response

I’m a fan of C# myself, so I’ll post more sample code in a few days when I get back. :smile:

Thanks,
David

OK I think I get how to make the call now I guess but what is the format? For the two LED over the web project.

I tried something like this with the core and token replaced properly and it failed. Ideas?

using (WebClient client = new WebClient())
{

            byte[] response = client.UploadValues("https://api.sparkio/v1/devices/0123456789abcdef01234567/led", new NameValueCollection()
   {
       { "access_token", "12341234123412341423412341234" },
       { "parms", "l1,HIGH" }
   });
        }

Ok Nevermind I got it… was a typo in the url. https://api.spark.io not https://api.sparkio

Got it working now.

Thanks

1 Like

Ah you beat me to it! Cool, glad you got it working!

I’m sure other C# users would appreciate any code samples you wanted to share :smile:

Thanks!
David

1 Like

Dave and All,

I am a beginner – and I just started checking this out today. I also cannot figure out where to plug in the API string for the sample “Controlling two LEDs from the Web”. Am I supposed to cut and paste to my browser as a URL? (I understand I am supposed to swap out my code I.D. and access token.) The other thing I tried unsuccessfully was to paste the API string into my Mac terminal program, but that didn’t work either.

Any help is appreciated! Thanks!
Mark

1 Like

I created an open source .NET library for three spark api. It covers all the device features.

I use it for all device features of http://atomiot.com .

Hi @mark3575

The controlling LEDs over the net demo uses a command line tool called “curl” that runs Windows, Linux, and Mac OS X. It simulates a web browser HTTP GET or POST request:

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/led \
  -d access_token=1234123412341234123412341234123412341234 \
  -d params=l1,HIGH

This is POST request similar to what you have when click on a form on a web page. POST sends data from the browser to a web server, in this case the api.spark.io server.

You should get curl for your kind of PC–it is a great debugging tool. Otherwise a POST request requires a bit of HTML. The normal way to go to a web page is via a HTTP GET request, which curl can also do. You see that in the examples that read Spark variables.

1 Like

Thank you. I appreciate it! I’ll install cURL for Mac OS X Mavericks 10.9.2.

Mark

1 Like

Thanks @bko! You beat me to it :slight_smile:

Thank you guys for the great efforts. exactly what i was looking for

1 Like