title: curl POST into C#
body: <p>I am trying to find a way how to transfer curl command to C#. </p>
  
  <p>What i need is to obtain token from api and save it into file C:\...\x.json\</p>
  
  <p>Then i want to declare token into variable, and use it into another curl POST request to take data to work with them. </p>
  
  <p>Curl:</p>
  
  <pre><code>curl -X POST "https://example.com" 
  -H "accept: application/json" 
  -H "Content-Type: application/json" 
  -d {"username":"username","password":"password"}
  </code></pre>
  
  <p>My current try:</p>
  
  <pre><code>static void Main(string[] args)
  {
      using (var httpClient = new HttpClient())
      {
          using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://example.com"))
          {
              request.Headers.TryAddWithoutValidation("Accept", "application/json");
  
              request.Content = new StringContent("{\"username\":\"username\",\"password\":\"password\"}", Encoding.UTF8, "application/json");
  
              var response = await httpClient.SendAsync(request);
          }
      }                        
  }
  </code></pre>
  
  <p>I tried several things, but none of them was working. This one is from curl.olsh.me but also i am getting some await error what i don't know what to do with it. (I am newbie in C#) : </p>
  
  <blockquote>
    <p>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'.</p>
  </blockquote>
  
  <p>curl -v output: </p>
  
  <ul>
  <li><ul>
  <li>Trying 10.0.0.0...</li>
  <li>TCP_NODELAY set</li>
  <li>Connected to example.com (10.0.0.0) port 443 (#0)</li>
  <li>ALPN, offering h2</li>
  <li>ALPN, offering http/1.1</li>
  <li>successfully set certificate verify locations:</li>
  <li>CAfile: C:\Users\lvrabel\Downloads\curl-7.60.0-win64-mingw\curl-7.60.0-win64-mingw\bin\curl-ca-bundle.crt
  CApath: none</li>
  <li>TLSv1.2 (OUT), TLS handshake, Client hello (1):</li>
  <li>TLSv1.2 (IN), TLS handshake, Server hello (2):</li>
  <li>TLSv1.2 (IN), TLS handshake, Certificate (11):</li>
  <li>TLSv1.2 (IN), TLS handshake, Server key exchange (12):</li>
  <li>TLSv1.2 (IN), TLS handshake, Server finished (14):</li>
  <li>TLSv1.2 (OUT), TLS handshake, Client key exchange (16):</li>
  <li>TLSv1.2 (OUT), TLS change cipher, Client hello (1):</li>
  <li>TLSv1.2 (OUT), TLS handshake, Finished (20):</li>
  <li>TLSv1.2 (IN), TLS handshake, Finished (20):</li>
  <li>SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256</li>
  <li>ALPN, server did not agree to a protocol</li>
  <li>Server certificate:</li>
  <li>subject: OU=Domain Control Validated; OU=PositiveSSL Wildcard; CN=*.example.com</li>
  <li>start date: Dec  1 00:00:00 2016 GMT</li>
  <li>expire date: Dec  1 23:59:59 2019 GMT</li>
  <li>subjectAltName: host "example.com" matched cert's "*.example.com"</li>
  <li>issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server</li>
  <li>SSL certificate verify ok.
  &lt; POST /cxf/IOTServices/v2/Login HTTP/1.1
  &lt; Host: example.com
  &lt; User-Agent: curl/7.60.0
  &lt; accept: application/json
  &lt; Content-Type: application/json
  &lt; Content-Length: 64
  &lt;</li>
  <li>upload completely sent off: 64 out of 64 bytes
  &lt; HTTP/1.1 200 OK
  &lt; Date: Mon, 24 Sep 2018 09:22:30 GMT
  &lt; Content-Type: application/json
  &lt; Date: Mon, 24 Sep 2018 09:22:31 GMT
  &lt; Connection: close
  &lt; Set-Cookie: TS0119a36f=015d568915873c94e852616fd92d5dd7a03a620f3cd2326781154684dfd7b31302f6fcb9822598d5a76466d0c6a25583fccbb30c7d; Path=/; Domain=.example.com
  &lt; Transfer-Encoding: chunked
  &lt;
  {"code":200,"status":"success","sessionId":"5cd92d80-bfdb-11e8-83c7-5b742f0c1244"}* Closing connection 0</li>
  <li>TLSv1.2 (OUT), TLS alert, Client hello (1):</li>
  </ul></li>
  </ul>