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

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

Add certificate on request with RestSharp

Ok, I got the solution. First of all, I had to stop using the .crt and the .key for the certificate. I have to get a .pfx. This can be done with openssl command (openssl documentation) openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt After creating the certificate, just add it to … 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

RestSharp serialization to JSON, object is not using SerializeAs attribute as expected

This is for @MaxiWheat and anyone else interested in how to use JSON.NET as the JSON serializer in a RestSharp request. Basically, I used the approach described in this blog post by Patrick Riley: // create the request var request = new RestRequest(yourUrlHere, Method.POST) { RequestFormat = DataFormat.Json }; // attach the JSON.NET serializer for … 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 JSON Parameter Posting

You don’t have to serialize the body yourself. Just do request.RequestFormat = DataFormat.Json; request.AddJsonBody(new { A = “foo”, B = “bar” }); // Anonymous type object is converted to Json body If you just want POST params instead (which would still map to your model and is a lot more efficient since there’s no serialization … Read more