-2

I am creating c# application from which I want to execute curl command.

Following is my command:

curl  -i -X POST -H "X-AUTH-TOKEN: xxxxxx" -H "Content-Type: application/json" -H "Accept: application/json" -d '{"query":"show tables;", "command_type": "PrestoCommand"}' "https://azure.qubole.com/api/v2/clusters/"

I want to pass parameters to the curl command from UI. and dont want to use the .bat or .exe file option.

Heta Desai
  • 57
  • 1
  • 11
  • 2
    Possible duplicate of [How To: Execute command line in C#, get STD OUT results](https://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results) – Dean Seo Jan 08 '18 at 06:22

1 Answers1

2

There are numerous ways to send an http request without curl.

Most common these days is the HttpClient class.

For example,

// at the top of the file
using System.Net.Http;

using(var client = new HttpClient())
{
 client.DefaultRequestHeaders.Add("X-AUTH-TOKEN", "xxxxxx");

 var result = await client.PostAsync("https://azure.qubole.com/api/v2/clusters/", new StringContent("{\"query\":\"show tables;\", \"command_type\": \"PrestoCommand\"}", Encoding.UTF8);
 var responseJson = await result.Content.ReadAsStringAsync();
}

Update When you are starting with c#, if you simply pasted this into Program.cs Main method, you would get the following error:

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

One way to solve it is to put all of the code above into a method like so

async Task DoWork()
{
 // my code above here
}

And then paste this to Main method:

DoWork().GetAwaiter().GetResult();
zaitsman
  • 8,984
  • 6
  • 47
  • 79
  • I tried this error it throws following error: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. – Heta Desai Jan 08 '18 at 07:34
  • 1
    That is because you copied it into the `Main` method. Either enable c# 7.1 features or change `await`s to `.GetAwaiter().GetResult()` – zaitsman Jan 08 '18 at 08:53