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 https://api.easypost.com/v2/api_keys \
  -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.

  //Initialization
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://api.easypost.com/v2/api_keys");
           
            //Our http method is GET.
            req.Method = "GET";

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

            //Some information about the response
            Debug.WriteLine(resp.StatusCode);
            Debug.WriteLine(resp.Server);

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

Open in new window

tango2009Asked:
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 https://api.easypost.com/v2/api_keys";

            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();

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

            proc.WaitForExit();
        }
    }
}

Open in new window

0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Is this what you want?
 //Initialization
String s = @"https://api.easypost.com/v2/api_keys -u" + <YOUR_API_KEY> ;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(s);
0
 
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:

http://onlinecurl.com/

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.
0
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 = "https://api.easypost.com/v2/api_keys";
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

0
 
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.
0
 
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.
0
 
Flabio GatesCommented:
Perhaps if you show your code, we can see what's wrong.
0
 
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,
http://onlinecurl.com/

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#
0
 
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.
0
 
Flabio GatesCommented:
Question inactive
0
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.