title: Understanding cURL Requests (C#)
body: <p>I am looking at some documentation that only shows examples of how to send web requests in cURL, however I'm using .NET and am trying to understand how I would send the same requests but using c#</p>
  
  <p>Some examples of the requests I'm trying to send:</p>
  
  <pre><code>curl -X POST -H "Content-Type: application/json" -u "{username}":"{password}" -d @parameters.json "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2017-02-27"
  </code></pre>
  
  <p>with example parameters:</p>
  
  <pre><code>{
    "text": "IBM is an American multinational technology company headquartered in Armonk, New York, United States, with operations in over 170 countries.",
    "features": {
      "entities": {
        "emotion": true,
        "sentiment": true,
        "limit": 2
      },
      "keywords": {
        "emotion": true,
        "sentiment": true,
        "limit": 2
      }
    }
  }
  </code></pre>
  
  <p>I have made several different attempts to hit the API in C# with no success, always getting a <code>Bad Request</code> response so I must be doing something wrong. Here are some of the attempts I've made:</p>
  
  <p>Attempt 1:</p>
  
  <pre><code>string url = $"{Properties.Settings.Default.WatsonLanguageUnderstandingUrl}/v1/analyze?version=2017-02-27";
              HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  
              string auth = $"{Properties.Settings.Default.WatsonLanguageUnderstandingUsername}:{Properties.Settings.Default.WatsonLanguageUnderstandingPassword}";
              string auth64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
              string credentials = $"Basic {auth64}";
  
              httpWebRequest.Headers[HttpRequestHeader.Authorization] = credentials;
              httpWebRequest.ContentType = "application/json";
              httpWebRequest.Method = "POST";
  
              using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
              {
                  string json = JsonConvert.SerializeObject(new
                  {
                      features = new
                      {
                          entities = new
                          {
                              emotion = true,
                              sentiment = true,
                              limit = 2
                          },
                          keywords = new
                          {
                              emotion = true,
                              sentiment = true,
                              limit = 2
                          }
                      }
                  });
  
                  streamWriter.Write(json);
              }
  
              var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
              using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
              {
                  var result = streamReader.ReadToEnd();
              }
  </code></pre>
  
  <p>Attempt 2:</p>
  
  <pre><code>var authValue = new AuthenticationHeaderValue(
                  "Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{Properties.Settings.Default.WatsonLanguageUnderstandingUsername}:{Properties.Settings.Default.WatsonLanguageUnderstandingPassword}")));
  
              var client = new HttpClient() { DefaultRequestHeaders = {Authorization = authValue} };
  
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              var anonString = new
              {
                  text = "IBM is an American multinational technology company headquartered in Armonk, New York, United States, with operations in over 170 countries.",
                  features = new
                  {
                      entities = new
                      {
                          emotion = true,
                          sentiment = true,
                          limit = 2
                      },
                      keywords = new
                      {
                          emotion = true,
                          sentiment = true,
                          limit = 2
                      }
                  }
              };
              string json = JsonConvert.SerializeObject(anonString);
  
              HttpResponseMessage blah = await client.PutAsync($"{ Properties.Settings.Default.WatsonLanguageUnderstandingUrl}/v1/analyze?version=2017-02-27", new StringContent(json)).ConfigureAwait(false);
  </code></pre>
  
  <p>Attempt 3:</p>
  
  <pre><code>Dictionary&lt;string, string&gt; parameters = new Dictionary&lt;string, string&gt;
              {
                  { "version", "2017-02-27" },
                  { "features", "sentiment" }
              };
              string queryParameters = DictToString(parameters);
              var thing = new {text = angry };
              string json = JsonConvert.SerializeObject(thing);
  
              string url = $"{Properties.Settings.Default.WatsonLanguageUnderstandingUrl}/v1/analyze?{queryParameters}";
  
              string result;
              using (WebClient client = new WebClient())
              {
                  client.UseDefaultCredentials = true;
                  client.Credentials = new NetworkCredential(Properties.Settings.Default.WatsonLanguageUnderstandingUsername, Properties.Settings.Default.WatsonLanguageUnderstandingPassword);
                  client.Headers[HttpRequestHeader.ContentType] = "application/json";
                  result = client.UploadString(url, "POST", json);
              }
  </code></pre>
  
  <p>Where my <code>DictToString()</code> method looks like:</p>
  
  <pre><code>private static string DictToString(Dictionary&lt;string, string&gt; dict)
          {
              StringBuilder builder = new StringBuilder();
  
              foreach (KeyValuePair&lt;string, string&gt; kvp in dict)
              {
                  builder.Append(kvp.Key + "=" + kvp.Value + "&amp;");
              }
  
              return builder.ToString();
          }
  </code></pre>
  
  <p>Note that attempt 3 works and returns an ok request, but I can't get attempt 3 to work with all those example parameters and more.</p>
  
  <p>Any help into why my requests aren't working would be great! Thanks</p>