Search code examples
c#.nethttpwebrequestapi-key

How do I set the user property "-u" for an HttpWebRequest request


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.


Solution

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