Convert CURL command in WebRequest

0

I need to run the command below:

curl -X POST -u "{username}":"{password}" -H "Content-Type: application/json" 

-d '{
  "name": "my_environment",
  "description": "My environment"
}' "https://gateway.watsonplatform.net/discovery/api/v1/environments?version=2017-11-07"

So I wrote the code:

 public async Task<string> postMet(string commande)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://gateway.watsonplatform.net/discovery/api/v1/environments?version=2017-11-07");
            request.Method = "POST";
            request.Timeout = 1200;
            request.KeepAlive = true;
            request.Credentials = new NetworkCredential("xxx", "yyy");
            request.ContentType = @"application/json";
            request.Headers.Add("name", "AmbienteNewOnee");
            request.Headers.Add("description", "Ambiente AI do sistema Onee");

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                | SecurityProtocolType.Tls
                | SecurityProtocolType.Tls11
                | SecurityProtocolType.Tls12;

            WebResponse response = await request.GetResponseAsync();

            string content = await new StreamReader(response.GetResponseStream()).ReadToEndAsync();

            //response.Close();
            return content;
        }
        catch (TimeoutException)
        {
            return "TIMEOUT";
        }
        catch (Exception error)
        {
            return "Erro " + error;
        }

But I'm getting the error:

  

System.Net.WebException error: The remote server returned an error: (400) Bad Request.      at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)      at System.Threading.Tasks.TaskFactory 1.FromAsyncCoreLogic(IAsyncResult iar, Func 2 endFunction, Action 1 endAction, Task 1 promise, Boolean requiresSynchronization)   --- End of stack trace from previous location where exception was thrown ---      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task)      at System.Runtime.CompilerServices.TaskAwaiter'1.GetResult ()      at Onee.Classes.API.IBM_Watson.watson.d__0.MoveNext () in D: \ Projects \ TP Technology APP \ Onee \ Onee Beta 2.12.17.1 \ Onee \ Classes \ API \ IBM Watson \ watson.cs: line 31

API documentation link: Watson Discovery

    
asked by anonymous 02.12.2017 / 21:14

1 answer

0

Instead of writing the code, I installed the package available in github

dotnet-standard-sdk

a>

Note: you need framework 4.61

    
02.12.2017 / 21:45