How to POST request using RestSharp

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) { … Read more

Deserializing a json string with restsharp

There are sereval ways to do this. A very popular library to handle json is the Newtonsoft.Json. Probably you already have it on your asp.net project but if not, you could add it from nuget. Considering you have a response object, include the following namespaces and call the static method DeserializeObject<T> from JsonConvert class: using … Read more

RestSharp post request – Body with x-www-form-urlencoded values

this working for me, it was generator from postman var token = new TokenValidation() { app_id = CloudConfigurationManager.GetSetting(“appId”), secret = CloudConfigurationManager.GetSetting(“secret”), grant_type = CloudConfigurationManager.GetSetting(“grant_type”), Username = CloudConfigurationManager.GetSetting(“Username”), Password = CloudConfigurationManager.GetSetting(“Password”), }; var client = new RestClient($”{xxx}{tokenEndPoint}”); var request = new RestRequest(Method.POST); request.AddHeader(“content-type”, “application/x-www-form-urlencoded”); request.AddParameter(“application/x-www-form-urlencoded”, $”app_id={token.app_id}&secret={token.secret}&grant_type={token.grant_type}&Username={token.Username}&Password={token.Password}”, ParameterType.RequestBody); IRestResponse response = client.Execute(request); if (response.StatusCode != HttpStatusCode.OK) { … Read more

RestSharp print raw request and response headers

RestSharp doesn’t provide a mechanism to achieve exactly what you want and activating the .Net tracing is a bit overkilling IMO. For logging (debugging) purposes (something that I can leave turned on for a while in PROD for example) I have found this approach to be very useful (although it has some details on how … Read more

How do I get an OAuth 2.0 authentication token in C#

In Postman, click Generate Code and then in Generate Code Snippets dialog you can select a different coding language, including C# (RestSharp). Also, you should only need the access token URL. The form parameters are then: grant_type=client_credentials client_id=abc client_secret=123 Code Snippet: /* using RestSharp; // https://www.nuget.org/packages/RestSharp/ */ var client = new RestClient(“https://service.endpoint.com/api/oauth2/token”); var request = … Read more

RestSharp simple complete example [closed]

Pawel Sawicz .NET blog has a real good explanation and example code, explaining how to call the library; GET: var client = new RestClient(“192.168.0.1”); var request = new RestRequest(“api/item/”, Method.GET); var queryResult = client.Execute<List<Items>>(request).Data; POST: var client = new RestClient(“http://192.168.0.1”); var request = new RestRequest(“api/item/”, Method.POST); request.RequestFormat = DataFormat.Json; request.AddBody(new Item { ItemName = someName, … Read more

tech