method not allowed

Erum Mirza 6 Reputation points
2021-10-04T16:55:51.537+00:00

while working with identity server . i used restsharp .its giving me an error
"method not allowed" as a response, can any body help me

Developer technologies ASP.NET ASP.NET Core
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-10-04T18:44:00.103+00:00

    the error means routing can not find an endpoint that matches the path, verb, query string and post data combination.

    0 comments No comments

  2. Erum Mirza 6 Reputation points
    2021-10-05T08:14:58.147+00:00

    using RestSharp;

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)48
    | (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    var client = new RestClient("https://localhost:4009/connect/token");
    var request = new RestRequest(Method.POST);
    request.AddHeader("content-type", "application/x-www-form-urlencoded");
    request.AddParameter("application/x-www-form-urlencoded"
    , "grant_type=client_credentials&client_id="m2m2"&client_secret="uykykjh"&audience=Api&scope=Api", ParameterType.RequestBody);
    IRestResponse response =await client.ExecuteAsync(request);
    var result = JsonConvert.DeserializeObject<ResponseMessage>(response.Content);
    //api call
    //the same code with microsoft httpclient api works here
    HttpClient apiclient2 = new HttpClient();
    apiclient2.DefaultRequestHeaders.Authorization
    = new AuthenticationHeaderValue("Bearer", result.access_token);
    var result2= await apiclient2.GetAsync("https://localhost:44347/weatherforecast/notedited/1");
    var content = await result2.Content.ReadAsStringAsync();

            /*************this code is not working*****************************/
          //  api call
            var apiclient = new RestClient("https://localhost:44347/weatherforecast/notedited/1");
            var apirequest = new RestRequest(Method.GET);
    
            apirequest.AddHeader("content-type", "application/json");
            apirequest.AddHeader("authorization", string.Format( "Bearer {0}" , result.access_token));
           // IRestResponse apiresponse =  apiclient.Execute(request);
            //the error appears here "Method not allowed"******************/
            IRestResponse apiresponse = await apiclient.ExecuteAsync(request);
            /*************this code is not working ends*****************************/
    
    0 comments No comments

  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-10-05T14:50:33.92+00:00

    I don’t use restsharp, but why are you setting the content type on a get?

    0 comments No comments

  4. Anonymous
    2021-10-07T05:26:09.86+00:00

    Hi @Erum Mirza ,

    ...  
    var request = new RestRequest(Method.POST);  
    ...  
    //api call  
    //the same code with microsoft httpclient api works here  
    HttpClient apiclient2 = new HttpClient();  
    apiclient2.DefaultRequestHeaders.Authorization  
    = new AuthenticationHeaderValue("Bearer", result.access_token);  
    var result2= await apiclient2.GetAsync("https://localhost:44347/weatherforecast/notedited/1");  
    var content = await result2.Content.ReadAsStringAsync();  
    
         /*************this code is not working*****************************/  
       //  api call  
         var apiclient = new RestClient("https://localhost:44347/weatherforecast/notedited/1");  
         var apirequest = new RestRequest(Method.GET);  
              
         apirequest.AddHeader("content-type", "application/json");  
         apirequest.AddHeader("authorization", string.Format( "Bearer {0}" , result.access_token));  
        // IRestResponse apiresponse =  apiclient.Execute(request);  
         //the error appears here "Method not allowed"******************/  
         IRestResponse apiresponse = await apiclient.ExecuteAsync(request);  
         /*************this code is not working ends*****************************/  
    

    The cause of the problem is that you used the wrong RestRequest, which using different request method. Please check the code above: at lines 2, 15 and 21.

    From the HttpClient request, we can know that the weatherforecast/notedited/1 end point should be accessed via the Get method.

    IRestResponse apiresponse = await apiclient.ExecuteAsync(request);  
    

    But from the above code, when using the RestClient, you are using the request (which is the Post method), you should change it to apirequest (this is the Get method). Change the code as below:

    IRestResponse apiresponse = await apiclient.ExecuteAsync(apirequest);  
    

    Besides, the above change still not working, try to remove the follow code:

         apirequest.AddHeader("content-type", "application/json");  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.