Getting Started

Mark Sheppard edited this page Apr 7, 2017 · 17 revisions

Basic Usage

If you only have a small number of one-off requests to make to an API, you can use RestSharp like so:

using RestSharp;

var client = new RestClient();
client.BaseUrl = new Uri("");
client.Authenticator = new HttpBasicAuthenticator("username", "password");

var request = new RestRequest();
request.Resource = "statuses/friends_timeline.xml";

IRestResponse response = client.Execute(request);

IRestResponse contains all of the information returned from the remote server. You have access to the headers, content, HTTP status and more. It is recommended that you use the generic version Execute to automatically deserialize the response into .NET classes. See Recommended Usage for an example.

Note about error handling

If there is a network transport error (network is down, failed DNS lookup, etc), RestResponse.ResponseStatus will be set to ResponseStatus.Error, otherwise it will be ResponseStatus.Completed. If an API returns a 404, ResponseStatus will still be Completed. If you need access to the HTTP status code returned you will find it at RestResponse.StatusCode. The Status property is an indicator of completion independent of the API error handling.

Recommended Usage >>