Skip to content

Commit e261b82

Browse files
committed
Adding the docs page
1 parent c817967 commit e261b82

File tree

9 files changed

+143
-68
lines changed

9 files changed

+143
-68
lines changed

docs/.idea/.gitignore

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.idea/dictionaries/alexey.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.idea/docs.iml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.vuepress/config.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,40 @@ module.exports = {
1515
title: "Getting Started",
1616
path: "/getting-started/",
1717
collapsable: false,
18+
children: [
19+
"",
20+
"getting-started"
21+
]
1822
},
1923
{
2024
title: "Using RestSharp",
2125
path: "/usage/",
2226
collapsable: false,
2327
children: [
24-
"/usage/serialization",
25-
"/usage/files",
26-
"/usage/authenticators",
27-
"/usage/parameters",
28-
"/usage/exceptions"
28+
"serialization",
29+
"files",
30+
"authenticators",
31+
"parameters",
32+
"exceptions"
2933
]
3034
},
3135
{
3236
title: "Got stuck?",
3337
path: "/get-help/",
3438
collapsable: false,
3539
children: [
36-
"/get-help/faq"
40+
"faq"
3741
]
3842
},
3943
{
4044
title: "Reference",
4145
path: "/api/",
4246
collapsable: true,
4347
children: [
44-
"/api/RestSharp",
45-
"/api/RestSharp.Serializers.NewtonsoftJson",
46-
"/api/RestSharp.Serializers.SystemTextJson",
47-
"/api/RestSharp.Serializers.Utf8Json",
48+
"RestSharp",
49+
"RestSharp.Serializers.NewtonsoftJson",
50+
"RestSharp.Serializers.SystemTextJson",
51+
"RestSharp.Serializers.Utf8Json",
4852
]
4953
}
5054
],

docs/getting-started/README.md

+4-58
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,7 @@
1-
# Getting Started
1+
# Introduction
22

3-
First, add the NuGet package to your project:
3+
The main purpose of RestSharp is to make synchronous and asynchronous calls to remote resources over HTTP. As the name suggests, the main audience of RestSharp are developers tha use REST APIs. However, RestSharp can call any API over HTTP (but not HTTP/2), as long as you have the resource URI and request parameters that you want to send comply with W3C HTTP standards.
44

5-
```
6-
dotnet add package RestSharp
7-
```
8-
9-
## Basic Usage
10-
11-
If you only have a small 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 of the information returned from the remote server.
26-
You have access to the headers, content, HTTP status and more.
27-
28-
It is recommended that you use 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
53-
HTTP methods return the `Task<T>` instead of `Task<IRestResponse<T>>`. Because it
54-
means that you won't get an error response if the request fails, those methods
55-
throw an exception.
56-
57-
All `ExecuteAsync` overloads, however, behave in the same way as `Execute` and return
58-
the `IRestResponse` or `IRestResponse<T>`.
59-
60-
Read [here](../usage/exceptions.md) about how RestSharp handles exceptions.
5+
One of the main challenges of using HTTP APIs for .NET developers is to work with requests and responses of different kinds and translate them to complex C# types. RestSharp can take care of serializing the request body to JSON or XML and deserialize the response. It can also form a valid request URI based on different parameter kinds - path, query, form or body.
616

7+
Check the [Getting started](getting-started.md) page to learn about using RestSharp in your application.
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)