-2

I tried to do an api request to https://api.github.com/users and I got this forbidden response returned. As it doesn't need authentication, I don't know why I got that 403 returned.

{StatusCode: 403, Version: 1.0, Content: System.Net.Http.StreamContent, 
Headers:

RequestMessage = {Method: GET, RequestUri: 'https://api.github.com/users', 
Version: 1.1, Content: <null>, Headers:
{
   Accept: application/json
}}

ReasonPhrase = "Forbidden"
IsSuccessStatusCode = false

This following is my code.

namespace ConsumingWebAapiRESTinMVC.Controllers
{
public class HomeController : Controller
    {
    //Hosted web API REST Service base url  
    string Baseurl = "https://api.github.com/users";
    public async Task<ActionResult> Index()
    {
        List<Employee> EmpInfo = new List<Employee>();

        using (var client = new HttpClient())
        {
            //Passing service base url  
            client.BaseAddress = new Uri(Baseurl);

            client.DefaultRequestHeaders.Clear();
            //Define request data format  
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Sending request to find web api REST service resource GetAllEmployees using HttpClient  
            HttpResponseMessage Res = await client.GetAsync(Baseurl);

            //Checking the response is successful or not which is sent using HttpClient  
            if (Res.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api   
                var EmpResponse = Res.Content.ReadAsStringAsync().Result;

                //Deserializing the response recieved from web api and storing into the Employee list  
                EmpInfo = JsonConvert.DeserializeObject<List<Employee>>(EmpResponse);

            }
            //returning the employee list to view  
            return View(EmpInfo);
        }
    }
}

}

Khant Htet Naing
  • 128
  • 1
  • 3
  • 11

1 Answers1

2

If you check the actual response text Res.Content.ReadAsStringAsync().Result then you would have seen the following error message:

Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.

You need to add a User-Agent header.

client.DefaultRequestHeaders.Add("User-Agent", "C# App");

But more generally, you need to improve your logging so that you are seeing the real reasons things aren't working.

RB.
  • 36,301
  • 12
  • 91
  • 131
  • Hi there, I see this is the accepted answer. I'm facing the exact problem but wth Express js. I'm making a simple REST api end point. Where should i add the above header you specified in your answer. Please see my code: `router.get('/github', async function (req, res) { https.get('https://api.github.com/v3/users/tmtanzeel', function (apiRes) { apiRes.pipe(res); }).on('error', (e) => { console.error(e); res.status(500).send('Something went wrong'); }); });` – Tanzeel Oct 01 '21 at 16:45
  • I'm getting this `(node:22326) UnhandledPromiseRejectionWarning: ReferenceError: DefaultRequestHeaders is not defined` – Tanzeel Oct 01 '21 at 16:46