Results 1 to 2 of 2

Thread: rest sharp code is not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    14

    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,727

    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.

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