Link to home
Start Free TrialLog in
Avatar of tango2009
tango2009

asked on

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

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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);
Avatar of tango2009
tango2009

ASKER

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

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.
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.
Perhaps if you show your code, we can see what's wrong.
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#
Can you run curl successfully locally? I.e., from your machine? If so, then we can call curl from c# then read its output.
ASKER CERTIFIED SOLUTION
Avatar of Flabio Gates
Flabio Gates

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Question inactive