1

I have a problem about 502 gateway error in my production server. I try to develop an application which calls azure rest endpoints. I get response with Postman by sending the endpoint which has following documentation:

https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/classification-nodes/get?view=azure-devops-rest-6.0

While Postman or Invoke-RestMethod work well with azure endpoint, but API, developed by me, based on HttpClient/RestSharp give an 502 Bad gateway error. HttpClient code part can be found below:

var personalaccesstoken = "<my-token>";

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "<my-email-address>", personalaccesstoken))));

    HttpRequestMessage requestMessage = new HttpRequestMessage();
    requestMessage.Method = HttpMethod.Get;
    requestMessage.RequestUri = new Uri("<my-azure-server-address>/_apis/wit/classificationNodes/Areas?$depth=100");
    using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
    {
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseBody);
        return responseBody;
    }
}

How can I solve the problem or which tool can I use to find the exact problem?

Thank you in advance:

mrtkprc
  • 57
  • 7
  • I see you're using Basic authentication and Azure usually requires Bearer authentication. Have you tried using that instead? And also, do refactor the usage of your HttpClient. You should never have an HttpClient in a using statement. – TheHvidsten Mar 08 '22 at 18:47
  • Postman also has Basic authorization configuration to make a request. I can see Basic Authorization like Basic xyz... in Header tab of Postman. – mrtkprc Mar 08 '22 at 19:00
  • Are you missing the *api-version* URI parameter? From the doc you linked, this is a required parameter. Sidenote: by using `client.SendAsync(requestMessage).Result`, the calling thread will be blocked until the async operation completed. Tip: use await instead – johnmoarr Mar 08 '22 at 19:36
  • Were you able to figure out the fix? – Tushit Nagda Nov 09 '22 at 06:50
  • The issue was about defined proxy configuration at pc. – mrtkprc Nov 12 '22 at 15:51
  • can you share the code please ? I have same issue @mrtkprc – Georges Khater Dec 07 '22 at 15:29

0 Answers0