Results 1 to 4 of 4

Thread: rest sharp code is not working

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2019
    Posts
    28

    rest sharp code is not working

    below code is not working giving me an error "method not allowoed"

    Code:
     
    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*****************************/

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    Re: rest sharp code is not working

    Thread moved from the CodeBank forum (which is for working examples, not questions) to the ASP.NET And ASP.NET Core forum.

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: rest sharp code is not working

    If you are getting the method is not allowed this mean that the API is not having that method.

    Code:
    // IRestResponse apiresponse =  apiclient.Execute(request);
    Looks like you are doing a post method to create element which may not supported .
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    Addicted Member
    Join Date
    Dec 2007
    Location
    Atlanta, Georgia
    Posts
    139

    Re: rest sharp code is not working

    Did you get this resolved?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width