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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Flabio GatesCommented:
Question inactive
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.