Getting Started

Bill Call edited this page Nov 4, 2017 · 18 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;
using RestSharp.Authenticators;

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 >>

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.