Reputation: 117
I am calling a Rest API using a basic http authentication
public string Get(string LabName)
{
string userName = ConfigurationManager.AppSettings["username"];
string password = ConfigurationManager.AppSettings["password"];
string BaseURL = ConfigurationManager.AppSettings["BaseURL"];
using (var client = new HttpClient())
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback( delegate { return true; });
Uri uri = new Uri(BaseURL);
client.BaseAddress = uri;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var byteArray = Encoding.ASCII.GetBytes(userName+":"+password);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
string clarity_URL = BaseURL + "api/v2/labs?name=" + LabName;
var response = client.GetAsync(clarity_URL).Result;
string responseString = response.Content.ReadAsStringAsync().Result;
return responseString;
}
When I debug the code throws error on the line response like
Can anyone please suggest me what could be the issue.
Upvotes: 1
Views: 4600
Reputation: 2270
A 500 Error usually means there is a problem with the API Server.
It would be a good idea to check the specific endpoint for any errors then check again with this code.
If you are checking against a web call that is working correctly, please ensure that the request method (GET / POST / PUT) is correctly aligned and the parameters match.
Upvotes: 2