Share via


Curl integrated in c#

Question

Wednesday, April 25, 2018 9:55 AM

Hello !

I need help please!

How can I run this code in Curl in an application developed in C #

curl -i -H "Content-Type: application/json" -X POST -d '{"node_id":"3","value":"100"}'http://10.194.*****/dimmers/set_level

This code starts when i press a button ( to light a lamp via a Raspberry web service)

All replies (2)

Wednesday, April 25, 2018 2:16 PM

curl is just a commandline wrapper around an HTTP call. In .NET you'd use HttpClient to do the same thing.

var client = new HttpClient()
{
   BaseAddress = "http:10.194.*****"
};

var myData = new {
   node_id = 3,
   value = 100
};

//In an async/await method
var response = await client.PostAsJsonAsync("dimmers/set_level", myData);

If you really want to rely on an external command like curl (which must be installed into a path that is accessible by the system) then use Process.Start

Michael Taylor http://www.michaeltaylorp3.net


Sunday, April 29, 2018 2:14 PM

Hi Trabelsi Amine,

You wouldn't call cURL directly, rather, you'd use one of the following options.

HttpWebRequest/HttpWebResponse
WebClient
HttpClient (available from .NET 4.5 on)

Here is the example in StackOverFlow for your reference.

https://stackoverflow.com/questions/7929013/making-a-curl-call-in-c-sharp

You could try to Execute CURL Command using C#. Please refer to the example in the link below.

https://www.c-sharpcorner.com/code/1716/execute-curl-command-using-C-Sharp.aspx

Best Regards,

Wendy

Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].