Skip to content

Commit 747befe

Browse files
committed
AddParameter hints
1 parent d8ec481 commit 747befe

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/v107/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,35 @@ var client = new RestClient(options);
3939

4040
You can still change serializers and add default parameters to the client.
4141

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:
62+
63+
```csharp
64+
request.AddHeader("Content-Type", "application/json");
65+
request.AddHeader("Accept", "application/json");
66+
```
67+
68+
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+
4271
### Making requests
4372

4473
The `IRestRequest` interface is deprecated. You will be using the `RestRequest` class instance.
@@ -143,6 +172,8 @@ public class GitHubClient {
143172

144173
Do not use one instance of `RestClient` across different API clients.
145174

175+
This documentation contains the complete example of a [Twitter API client](../usage.md), which you can use as a reference.
176+
146177
## Presumably solved issues
147178

148179
The next RestSharp version presumably solves the following issues:

0 commit comments

Comments
 (0)