cUrl C# question

I am trying to send this cUrl command through my C# code but I am not sure how to properly send the api key part

cUrl Command

curl -X GET \
  -u <YOUR_API_KEY>:

This is code I am trying to use but I am not sure how to send the api key through the C# code below.

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"");
            //Our http method is GET.
            req.Method = "GET";

            //Get the response.
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            //Some information about the response

            //Now, we read the response (the string), and output it.
            StreamReader result = new StreamReader(resp.GetResponseStream());

Open in new window

Who is Participating?
Flabio GatesCommented:
Assuming you've downloaded curl and have added it to your system PATH variable:
namespace ConsoleApplication1
    using System.Diagnostics;

    class Program
        static void Main(string[] args)
            Process proc = new Process();
            proc.StartInfo.FileName = "curl.exe";

            // use your actual API key!
            proc.StartInfo.Arguments = "-u YOUR_API_KEY -X GET";

            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;

            // store your JSON in a variable
            var s = proc.StandardOutput.ReadToEnd();


Open in new window

AndyAinscowFreelance programmer / ConsultantCommented:
Is this what you want?
String s = @" -u" + <YOUR_API_KEY> ;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
tango2009Author Commented:
If I build it as one string like above then I get the following error

The remote server returned an error: (404) Not Found.

If I do not send the api key then I get the unauthorized error as I am not sending it which is expected.

I can send the request successfully using the below web page:

I enter the url and add an option of --user(-u)  with the api key as its value, this works great.

My problem is just replicating this in C# I am unsure how to send the --user(-u) value correctly.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Flabio GatesCommented:
try something like:
string uri = "";
string YOUR_API_KEY = @"foo:bar";
var a = YOUR_API_KEY.Split(':');
string user = a[0];
string password = (a.Length > 1) ? a[1] : "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Credentials = new NetworkCredential(user, password);
WebResponse response= req.GetResponse();

Open in new window

tango2009Author Commented:
Hi Flabio,

Thanks for your reply, I have tried passing it as the network credentials like above but having no luck.

I get the authorization error still.
Flabio GatesCommented:
Does the curl command-line work? If so then we can run that from c# then read the output. If curl fails then your token is wrong.
Flabio GatesCommented:
Perhaps if you show your code, we can see what's wrong.
tango2009Author Commented:
The curl command line does work, it seems the issue is how I can replicate it in C#.

I used this site for example,

I can send the command correctly if I add the api key as --user(-u). I get the proper keys returned to me.

I just don't know how to replicate this in C#
Flabio GatesCommented:
Can you run curl successfully locally? I.e., from your machine? If so, then we can call curl from c# then read its output.
Flabio GatesCommented:
Question inactive
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.