How to use OAuth2 in RestSharp

See RFC 6749 – 4.4.2. Client Credentials – Access Token Request Here is the basic format of the request POST /token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded grant_type=client_credentials Your cURL request curl -H “Accept: application/json” \ -d grant_type=client_credentials \ client-app:[email protected]/myapi/oauth/token The reason your cURL command works Default Content-Type (if not specified) with POST … Read more

RestSharp Timeout not working

Solution (Version 107+) var options = new RestClientOptions(“baseURL”) { ThrowOnAnyError = true, Timeout = 1000 // 1 second – thanks to @JohnMc }; var client = new RestClient(options); Older Versions: to alter the default time out to: 5 seconds – for example – (i.e. 5000 milliseconds): var client = new RestClient(“BaseUrl”); client.Timeout = 5000; // … Read more

How should I implement ExecuteAsync with RestSharp on Windows Phone 7?

Old question but if you are using C# 5 you can have a generic execute class by creating a TaskCompleteSource that resturns a Task of T. Your code could look like this: public Task<T> ExecuteAsync<T>(RestRequest request) where T : new() { var client = new RestClient(); var taskCompletionSource = new TaskCompletionSource<T>(); client.BaseUrl = BaseUrl; client.Authenticator … Read more

Are these the main differences between RestSharp and ServiceStack’s Client Code? [closed]

As the project lead of ServiceStack I can list some features of the ServiceStack Service clients: The ServiceStack Service Clients are opinionated in consuming ServiceStack web services and its conventions. i.e. They have built-in support for structured validation and error handling as well as all clients implement the same interface so you can have the … Read more

Set ‘Content-Type’ header using RestSharp

The solution provided on my blog is not tested beyond version 1.02 of RestSharp. If you submit a comment on my answer with your specific issue with my solution, I can update it. var client = new RestClient(“http://www.example.com/where/else?key=value”); var request = new RestRequest(); request.Method = Method.POST; request.AddHeader(“Accept”, “application/json”); request.Parameters.Clear(); request.AddParameter(“application/json”, strJSONContent, ParameterType.RequestBody); var response = … Read more

RestSharp get full URL of a request

To get the full URL use RestClient.BuildUri() Specifically, in this example use client.BuildUri(request): RestClient client = new RestClient(“http://www.some_domain.com”); RestRequest request = new RestRequest(“some/resource”, Method.GET); request.AddParameter(“some_param_name”, “some_param_value”, ParameterType.QueryString); IRestResponse<ResponseData> response = client.Execute<ResponseData>(request); var fullUrl = client.BuildUri(request);

RestSharp HttpBasicAuthentication – example

new SimpleAuthenticator(“username”, username, “password”, password) did NOT work with me. The following however worked: var client = new RestClient(“http://example.com”); client.Authenticator = new HttpBasicAuthenticator(userName, password); var request = new RestRequest(“resource”, Method.GET); client.Execute(request);

How to use restsharp to download file

With RestSharp, it’s right there in the readme: var client = new RestClient(“http://example.com”); client.DownloadData(request).SaveAs(path); With HttpClient, it’s a bit more involved. Have a look at this blog post. Another option is Flurl.Http (disclaimer: I’m the author). It uses HttpClient under the hood and provides a fluent interface and lots of convenient helper methods, including: await … Read more

tech