0

I can't figure out how to set the -u / --user property of the HttpWebRequest request. My goal here is to set the --user to an api key I have, similar to the command

curl https://api.stripe.com/v1/customers?limit=3 -u api_key***************:

I have tried setting the UserAgent propety instead, but it doesn't work:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
request.UserAgent = "api_key***************:";

I am trying to get a JSON response from a web API that requires an API key in the -u part. Thanks to anyone who can help.

Joshua Vandenbor
  • 509
  • 3
  • 6
  • 19

1 Answers1

1
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"api_key***************:")));

Apparently you use the -u or -user command in curl to use Basic authentication. It's strange to me that there is an api key in the username field but since there is a colon in your example that is probably what the API expects.

vdL
  • 263
  • 1
  • 8
  • I appreciate the answer, totally thought it would work as the curl command does. But unfortunately I'm still getting a 401 'Unauthorized' error. again the curl command is working with the exact same password. So strange to me – Joshua Vandenbor Aug 14 '19 at 13:56
  • I made a change to the answer, the "Basic " part was in the key parameter where the value "Authorization" should have been. – vdL Aug 14 '19 at 16:23