Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl POST into C#

Tags:

c#

curl

I am trying to find a way how to transfer curl command to C#.

What i need is to obtain token from api and save it into file C:\...\x.json\

Then i want to declare token into variable, and use it into another curl POST request to take data to work with them.

Curl:

curl -X POST "https://example.com" 
-H "accept: application/json" 
-H "Content-Type: application/json" 
-d {"username":"username","password":"password"}

My current try:

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);
        }
    }                        
}

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#) :

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'.

curl -v output:

    • Trying 10.0.0.0...
    • TCP_NODELAY set
    • Connected to example.com (10.0.0.0) port 443 (#0)
    • ALPN, offering h2
    • ALPN, offering http/1.1
    • successfully set certificate verify locations:
    • CAfile: C:\Users\lvrabel\Downloads\curl-7.60.0-win64-mingw\curl-7.60.0-win64-mingw\bin\curl-ca-bundle.crt CApath: none
    • TLSv1.2 (OUT), TLS handshake, Client hello (1):
    • TLSv1.2 (IN), TLS handshake, Server hello (2):
    • TLSv1.2 (IN), TLS handshake, Certificate (11):
    • TLSv1.2 (IN), TLS handshake, Server key exchange (12):
    • TLSv1.2 (IN), TLS handshake, Server finished (14):
    • TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
    • TLSv1.2 (OUT), TLS change cipher, Client hello (1):
    • TLSv1.2 (OUT), TLS handshake, Finished (20):
    • TLSv1.2 (IN), TLS handshake, Finished (20):
    • SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
    • ALPN, server did not agree to a protocol
    • Server certificate:
    • subject: OU=Domain Control Validated; OU=PositiveSSL Wildcard; CN=*.example.com
    • start date: Dec 1 00:00:00 2016 GMT
    • expire date: Dec 1 23:59:59 2019 GMT
    • subjectAltName: host "example.com" matched cert's "*.example.com"
    • issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server
    • SSL certificate verify ok. < POST /cxf/IOTServices/v2/Login HTTP/1.1 < Host: example.com < User-Agent: curl/7.60.0 < accept: application/json < Content-Type: application/json < Content-Length: 64 <
    • upload completely sent off: 64 out of 64 bytes < HTTP/1.1 200 OK < Date: Mon, 24 Sep 2018 09:22:30 GMT < Content-Type: application/json < Date: Mon, 24 Sep 2018 09:22:31 GMT < Connection: close < Set-Cookie: TS0119a36f=015d568915873c94e852616fd92d5dd7a03a620f3cd2326781154684dfd7b31302f6fcb9822598d5a76466d0c6a25583fccbb30c7d; Path=/; Domain=.example.com < Transfer-Encoding: chunked < {"code":200,"status":"success","sessionId":"5cd92d80-bfdb-11e8-83c7-5b742f0c1244"}* Closing connection 0
    • TLSv1.2 (OUT), TLS alert, Client hello (1):
like image 390
LukasV Avatar asked Sep 21 '18 11:09

LukasV


People also ask

How do I send a POST request with the curl command?

See the default CURL syntax for sending a POST request below. The -X parameter specifies the HTTP method for sending your request. In our case, we are using the POST method. To make a basic POST request with the Curl command, execute the command below on your Terminal. We can use the -d parameter to send additional data in a POST request.

How to post a form with different content type in curl?

By default, Curl sends an HTTP POST request and posts the provided form data with the application/x-www-form-urlencoded content type. You can use the-H command line parameter to post a form with a different content type and pass the desired data type there, for example, "Content-Type: multipart/form-data".

What is the use of curl-X post option?

curl -X POST [options] [URL] The -X option specifies which HTTP request method will be used when communicating with the remote server. The type of the request body is indicated by its Content-Type header. Generally, a POST request is sent via an HTML form.

How do I send data from curl?

When the -F option is used, curl sends the data using the multipart/form-data Content-Type. Another way to make a POST request is to use the -d option.


1 Answers

use this website https://curl.olsh.me/

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);
    }
}
like image 108
Jin Thakur Avatar answered Sep 22 '22 02:09

Jin Thakur