Search code examples
c#curlgithub-apihttp-accept-header

C# GitHub SCIM API // Difference between cURL and HttpClient


I'm facing a strange error with the usage of the GITHUB API. When I contact them with cURL it's like:

curl.exe -H "Accept: application/vnd.github.cloud-9-preview+json+scim" -H "Authorization: Bearer TOKEN" https://api.github.com/scim/v2/organizations/[ORG]/Users

When I try to take it to C#, if became:

 using (var cl = new HttpClient())
        {
            cl.DefaultRequestHeaders.Add("Accept", "application/vnd.github.cloud-9-preview+json+scim");
            cl.DefaultRequestHeaders.Add("Authorization", "Bearer " + "TOKEN");
            var val = cl.GetStringAsync("https://api.github.com/scim/v2/organizations/[ORG]/Users").Result;
        }

When I run my cURL everything works fine but when I try the same on C# I got a 403 Error.

Could it be related to the "Accept" non standard field?


Solution

  • I find out that the GITHUB API requires to have the User-Agent header Set.

    Setting it as "curl" made it.

    using (var cl = new HttpClient())
            {
                cl.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.github.cloud-9-preview+json+scim"));
                cl.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "token");
                cl.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("curl", "7.46.0"));
                var val = cl.GetStringAsync("https://api.github.com/scim/v2/organizations/[ORG]/Users").Result;
            }