Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to POST request using RestSharp

I m trying to POST the request using RestSharp client as follows I m passing the Auth Code to following function

public void ExchangeCodeForToken(string code) {     if (string.IsNullOrEmpty(code))     {         OnAuthenticationFailed();     }     else     {                    var request = new RestRequest(this.TokenEndPoint, Method.POST);         request.AddParameter("code", code);         request.AddParameter("client_id", this.ClientId);         request.AddParameter("client_secret", this.Secret);         request.AddParameter("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");         request.AddParameter("grant_type", "authorization_code");         request.AddHeader("content-type", "application/x-www-form-urlencoded");          client.ExecuteAsync<AuthResult>(request, GetAccessToken);     } }  void GetAccessToken(IRestResponse<AuthResult> response) {     if (response == null || response.StatusCode != HttpStatusCode.OK                          || response.Data == null                           || string.IsNullOrEmpty(response.Data.access_token))     {         OnAuthenticationFailed();     }     else     {         Debug.Assert(response.Data != null);         AuthResult = response.Data;         OnAuthenticated();     } } 

But i am getting response.StatusCode = Bad Request. Can anyone help me for how do i POST the request using Restsharp client.

like image 433
Ashish Avatar asked Jul 09 '12 18:07

Ashish


People also ask

How do I request a RestSharp body?

The request body is a type of parameter. To add one, you can do one of these... req. AddBody(body); req.

How do you put on a RestSharp?

Use NuGet to install RestSharp. To do that Right click on the Project or Solution file in Solution Exporter and select Manage NuGet packages. Search for it in there and install it. work perfect thanks !

Does RestSharp use HttpClient?

Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.


2 Answers

My RestSharp POST method:

var client = new RestClient(ServiceUrl);  var request = new RestRequest("/resource/", Method.POST);  // Json to post. string jsonToSend = JsonHelper.ToJson(json);  request.AddParameter("application/json; charset=utf-8", jsonToSend, ParameterType.RequestBody); request.RequestFormat = DataFormat.Json;  try {     client.ExecuteAsync(request, response =>     {         if (response.StatusCode == HttpStatusCode.OK)         {             // OK         }         else         {             // NOK         }     }); } catch (Exception error) {     // Log } 
like image 56
David Avatar answered Sep 23 '22 01:09

David


This way works fine for me:

var request = new RestSharp.RestRequest("RESOURCE", RestSharp.Method.POST) { RequestFormat = RestSharp.DataFormat.Json }                 .AddBody(BODY);  var response = Client.Execute(request);  // Handle response errors HandleResponseErrors(response);  if (Errors.Length == 0) { } else { } 

Hope this helps! (Although it is a bit late)

like image 41
Kutyel Avatar answered Sep 25 '22 01:09

Kutyel