I want to make the following curl
call in my C# console application:
curl -d "text=This is a block of text" \ http://api.repustate.com/v2/demokey/score.json
I tried to do like the question posted here, but I cannot fill the properties properly.
I also tried to convert it to a regular HTTP request:
http://api.repustate.com/v2/demokey/score.json?text="This%20is%20a%20block%20of%20text"
Can I convert a cURL call to an HTTP request? If so, how? If not, how can I make the above cURL call from my C# console application properly?
To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.
Client URL, or just curl, is a command-line tool for transferring data using various network protocols. It is commonly used by developers to test various applications build on top of HTTP. That said, curl itself is just a wrapper around libcurl. The library is written in C and has well documented API.
libcurl is a free and easy-to-use client-side URL transfer library, supporting DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP.
libcurl is a library of functions that are provided with a C API, for applications written in C. You can easily use it from C++ too, with only a few considerations (see libcurl for C++ programmers).
Well, you wouldn't call cURL directly, rather, you'd use one of the following options:
HttpWebRequest
/HttpWebResponse
WebClient
HttpClient
(available from .NET 4.5 on)I'd highly recommend using the HttpClient
class, as it's engineered to be much better (from a usability standpoint) than the former two.
In your case, you would do this:
using System.Net.Http; var client = new HttpClient(); // Create the HttpContent for the form to be posted. var requestContent = new FormUrlEncodedContent(new [] { new KeyValuePair<string, string>("text", "This is a block of text"), }); // Get the response. HttpResponseMessage response = await client.PostAsync( "http://api.repustate.com/v2/demokey/score.json", requestContent); // Get the response content. HttpContent responseContent = response.Content; // Get the stream of the content. using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) { // Write the output. Console.WriteLine(await reader.ReadToEndAsync()); }
Also note that the HttpClient
class has much better support for handling different response types, and better support for asynchronous operations (and the cancellation of them) over the previously mentioned options.
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