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:
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.
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".
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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With