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.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/v107/README.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,35 @@ var client = new RestClient(options);
39
39
40
40
You can still change serializers and add default parameters to the client.
41
41
42
+
### RestClient lifecycle
43
+
44
+
Do not instantiate `RestClient` for each HTTP call. RestSharp creates a new instance of `HttpClient` internally, and you will get lots of hanging connections, and eventually exhaust the connection pool.
45
+
46
+
If you use a dependency-injection container, register your API client as a singleton.
47
+
48
+
### Body parameters
49
+
50
+
Beware that most of the code generators, like Postman C# code gen, generate code for RestSharp before v107, and that code is broken. Such code worked mostly due to obscurity of previous RestSharp versions API. For example, Postman-generated code tells you to add the content-type header, and the accept header, which in many cases is an anti-pattern. It also posts JSON payload as string, where RestSharp provides you with serialization and deserialization of JSON out of the box.
51
+
52
+
Therefore, please read the [Usage](../usage.md) page and follow our guidelines when using RestSharp v107+.
53
+
54
+
Some of the points to be aware of:
55
+
-`AddParameter("application/json", ..., ParameterType.RequestBody)` won't work, use `AddBody` instead, or better, `AddJsonBody`.
56
+
-`AddJsonBody("{ foo: 'bar' }")` won't work (and it never worked), use `AddStringBody`. `AddJsonBody` is for serializable objects, not for strings.
57
+
- If your `AddParameter(something, something, ParameterType.RequestBody)` doesn't work, try `AddBody` as it will do its best to figure out what kind of body you're adding.
58
+
59
+
### Headers
60
+
61
+
Lots of code out there that uses RestSharp has lines like:
This is completely unnecessary, and often harmful. The `Content-Type` header is the content header, not the request header. It might be different per individual part of the body when using multipart-form data, for example. RestSharp sets the correct content-type header automatically, based on your body format, so don't override it.
69
+
The `Accept` header is set by RestSharp automatically based on registered serializers. By default, both XML and JSON are supported. Only change the `Accept` header if you need something else, like binary streams, or plain text.
70
+
42
71
### Making requests
43
72
44
73
The `IRestRequest` interface is deprecated. You will be using the `RestRequest` class instance.
@@ -143,6 +172,8 @@ public class GitHubClient {
143
172
144
173
Do not use one instance of `RestClient` across different API clients.
145
174
175
+
This documentation contains the complete example of a [Twitter API client](../usage.md), which you can use as a reference.
176
+
146
177
## Presumably solved issues
147
178
148
179
The next RestSharp version presumably solves the following issues:
0 commit comments