|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +Before you can use RestSharp in your application, you need to add the NuGet package. You can do it using your IDE or the command line: |
| 4 | + |
| 5 | +``` |
| 6 | +dotnet add package RestSharp |
| 7 | +``` |
| 8 | + |
| 9 | +## Basic Usage |
| 10 | + |
| 11 | +If you only have a few number of one-off requests to make to an API, you can use RestSharp like so: |
| 12 | + |
| 13 | +```csharp |
| 14 | +using RestSharp; |
| 15 | +using RestSharp.Authenticators; |
| 16 | + |
| 17 | +var client = new RestClient("https://api.twitter.com/1.1"); |
| 18 | +client.Authenticator = new HttpBasicAuthenticator("username", "password"); |
| 19 | + |
| 20 | +var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json); |
| 21 | + |
| 22 | +var response = client.Get(request); |
| 23 | +``` |
| 24 | + |
| 25 | +`IRestResponse` contains all the information returned from the remote server. |
| 26 | +You have access to the headers, content, HTTP status and more. |
| 27 | + |
| 28 | +We recommend using the generic overloads like `Get<T>` to automatically deserialize the response into .NET classes. |
| 29 | + |
| 30 | +## Asynchronous Calls |
| 31 | + |
| 32 | +All synchronous methods have their asynchronous siblings, suffixed with `Async`. |
| 33 | + |
| 34 | +So, instead of `Get<T>` that returns `T` or `Execute<T>`, which returns `IRestResponse<T>`, |
| 35 | +you can use `GetAsync<T>` and `ExecuteAsync<T>`. The arguments set is usually identical. |
| 36 | +You can optionally supply the cancellation token, which by default is set to `CancellationToken.None`. |
| 37 | + |
| 38 | +For example: |
| 39 | + |
| 40 | +```csharp |
| 41 | +using RestSharp; |
| 42 | +using RestSharp.Authenticators; |
| 43 | + |
| 44 | +var client = new RestClient("https://api.twitter.com/1.1"); |
| 45 | +client.Authenticator = new HttpBasicAuthenticator("username", "password"); |
| 46 | + |
| 47 | +var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json); |
| 48 | + |
| 49 | +var timeline = await client.GetAsync<HomeTimeline>(request, cancellationToken); |
| 50 | +``` |
| 51 | + |
| 52 | +The most important difference, however, that async methods that are named after HTTP methods return the `Task<T>` instead of `Task<IRestResponse<T>>`. Because it means that you won't get an error response if the request fails, those methods |
| 53 | +throw an exception. |
| 54 | + |
| 55 | +All `ExecuteAsync` overloads, however, behave in the same way as `Execute` and return the `IRestResponse` or `IRestResponse<T>`. |
| 56 | + |
| 57 | +Read [here](../usage/exceptions.md) about how RestSharp handles exceptions. |
| 58 | + |
| 59 | +## Content type |
| 60 | + |
| 61 | +RestSharp supports sending XML or JSON body as part of the request. To add a body to the request, simply call `AddJsonBody` or `AddXmlBody` method of the `IRestRequest` instance. |
| 62 | + |
| 63 | +There is no need to set the `Content-Type` or add the `DataFormat` parameter to the request when using those methods, RestSharp will do it for you. |
| 64 | + |
| 65 | +RestSharp will also handle both XML and JSON responses and perform all necessary deserialization tasks, depending on th server response type. Therefore, you only need to add the `Accept` header if you want to deserialize the response manually. |
| 66 | + |
| 67 | +For example, only you'd only need these lines to make a request with JSON body: |
| 68 | + |
| 69 | +```csharp |
| 70 | +var request = new RestRequest("address/update") |
| 71 | + .AddJsonBody(updatedAddress); |
| 72 | +var respose = await client.PostAsync<AddressUpdateResponse>(request); |
| 73 | +``` |
| 74 | + |
| 75 | +## Response |
| 76 | + |
| 77 | +When you use `Execute` or `ExecuteAsync`, you get an instance of `IRestResponse` back that has the `Content` property, which contains the response as string. You can find other useful properties there, like `StatusCode`, `ContentType` and so on. If the request wasn't successful, you'd get a response back with `IsSuccessful` property set to `false` and the error explained in the `ErrorException` and `ErrorMessage` properties. |
| 78 | + |
| 79 | +When using typed `Execute<T>` or `ExecuteAsync<T>`, you get an instance of `IRestResponse<T>` back, which is identical to `IRestResponse` but also contains the `T Data` property with the deserialized response. |
| 80 | + |
| 81 | +Extensions like `Get<T>` and `GetAsync<T>` will not return the whole `IRestResponse<T>` but just a deserialized response. You might get `null` back if something goes wrong, and it can be hard to understand the issue. Therefore, when using typed extension methods, we suggest setting `IRestClient.ThrowOnAnyError` property to `true`. By doing that, you tell RestSharp to throw an exception when something goes wrong. You can then wrap the call in a `try`/`catch` block and handle the exception accordingly. To know more about how RestSharp deals with exceptions, please refer to the [Error handling](../usage/exceptions.md) page. |
0 commit comments