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/docs/usage/basics.md
-145
Original file line number
Diff line number
Diff line change
@@ -21,148 +21,3 @@ Essentially, RestSharp is a wrapper around `HttpClient` that allows you to do th
21
21
The best way to call an external HTTP API is to create a typed client, which encapsulates RestSharp calls and doesn't expose the `RestClient` instance in public.
22
22
23
23
You can find an example of a Twitter API client on the [Example](example.md) page.
24
-
25
-
26
-
## Handling responses
27
-
28
-
All `Execute{Method}Async` functions return an instance of `RestResponse`. Similarly, `Execute{Method}Async<T>` return a generic instance of `RestResponse<T>` where `T` is the response object type.
29
-
30
-
Response object contains the following properties:
|`Request`|`RestRequest`| Request instance that was used to get the response. |
35
-
|`ContentType`|`string?`| Response content type. `Null` if response has no content. |
36
-
|`ContentLength`|`long?`| Response content length. `Null` if response has no content. |
37
-
|`ContentEncoding`|`ICollection<string>`| Content encoding collection. Empty if response has no content. |
38
-
|`Content`|`string?`| Response content as string. `Null` if response has no content. |
39
-
|`IsSuccessfulStatusCode`|`bool`| Indicates if response was successful, so no errors were reported by the server. Note that `404` response code means success. |
40
-
|`ResponseStatus`|`None`, `Completed`, `Error`, `TimedOut`, `Aborted`| Response completion status. Note that completed responses might still return errors. |
41
-
|`IsSuccessful`|`bool`|`True` when `IsSuccessfulStatusCode` is `true` and `ResponseStatus` is `Completed`. |
42
-
|`StatusDescription`|`string?`| Response status description, if available. |
43
-
|`RawBytes`|`byte[]?`| Response content as byte array. `Null` if response has no content. |
44
-
|`ResponseUri`|`Uri?`| URI of the response, which might be different from request URI in case of redirects. |
45
-
|`Server`|`string?`| Server header value of the response. |
46
-
|`Cookies`|`CookieCollection?`| Collection of cookies received with the response, if any. |
47
-
|`Headers`| Collection of `HeaderParameter`| Response headers. |
48
-
|`ContentHeaders`| Collection of `HeaderParameter`| Response content headers. |
49
-
|`ErrorMessage`|`string?`| Transport or another non-HTTP error generated while attempting request. |
50
-
|`ErrorException`|`Exception?`| Exception thrown when executing the request, if any. |
51
-
|`Version`|`Version?`| HTTP protocol version of the request. |
52
-
|`RootElement`|`string?`| Root element of the serialized response content, only works if deserializer supports it. |
53
-
54
-
In addition, `RestResponse<T>` has one additional property:
|`Data`|`T?`| Deserialized response object. `Null` if there's no content in the response, deserializer failed to understand the response content, or if request failed. |
59
-
60
-
### JSON streaming APIs
61
-
62
-
For HTTP API endpoints that stream the response data (like [Twitter search stream](https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream)) you can use RestSharp with `StreamJsonAsync<T>`, which returns an `IAsyncEnumerable<T>`:
The main limitation of this function is that it expects each JSON object to be returned as a single line. It is unable to parse the response by combining multiple lines into a JSON string.
79
-
80
-
### Uploading files
81
-
82
-
To add a file to the request you can use the `RestRequest` function called `AddFile`. The main function accepts the `FileParameter` argument:
83
-
84
-
```csharp
85
-
request.AddFile(fileParameter);
86
-
```
87
-
88
-
You can instantiate the file parameter using `FileParameter.Create` that accepts a bytes array, or `FileParameter.FromFile`, which will load the file from disk.
89
-
90
-
There are also extension functions that wrap the creation of `FileParameter` inside:
The options specified in the snippet above usually help when you upload files with non-ASCII characters in their names.
120
-
121
-
### Downloading binary data
122
-
123
-
There are two functions that allow you to download binary data from the remote API.
124
-
125
-
First, there's `DownloadDataAsync`, which returns `Task<byte[]`. It will read the binary response to the end, and return the whole binary content as a byte array. It works well for downloading smaller files.
126
-
127
-
For larger responses, you can use `DownloadStreamAsync` that returns `Task<Stream>`. This function allows you to open a stream reader and asynchronously stream large responses to memory or disk.
128
-
129
-
## Blazor support
130
-
131
-
Inside a Blazor webassembly app, you can make requests to external API endpoints. Microsoft examples show how to do it with `HttpClient`, and it's also possible to use RestSharp for the same purpose.
132
-
133
-
You need to remember that webassembly has some platform-specific limitations. Therefore, you won't be able to instantiate `RestClient` using all of its constructors. In fact, you can only use `RestClient` constructors that accept `HttpClient` or `HttpMessageHandler` as an argument. If you use the default parameterless constructor, it will call the option-based constructor with default options. The options-based constructor will attempt to create an `HttpMessageHandler` instance using the options provided, and it will fail with Blazor, as some of those options throw thw "Unsupported platform" exception.
134
-
135
-
Here is an example how to register the `RestClient` instance globally as a singleton:
In this case, the call will be made to a WebAPI server hosted at `http://localhost:5104/weather`. Remember that if the WebAPI server is not hosting the webassembly itself, it needs to have a CORS policy configured to allow the webassembly origin to access the API endpoint from the browser.
Copy file name to clipboardexpand all lines: docs/docs/usage/client.md
+40
Original file line number
Diff line number
Diff line change
@@ -72,3 +72,43 @@ One way of doing it is to use `RestClient` constructors that accept an instance
72
72
73
73
Another option is to use a simple HTTP client factory as described [above](#simple-factory).
74
74
75
+
## Blazor support
76
+
77
+
Inside a Blazor webassembly app, you can make requests to external API endpoints. Microsoft examples show how to do it with `HttpClient`, and it's also possible to use RestSharp for the same purpose.
78
+
79
+
You need to remember that webassembly has some platform-specific limitations. Therefore, you won't be able to instantiate `RestClient` using all of its constructors. In fact, you can only use `RestClient` constructors that accept `HttpClient` or `HttpMessageHandler` as an argument. If you use the default parameterless constructor, it will call the option-based constructor with default options. The options-based constructor will attempt to create an `HttpMessageHandler` instance using the options provided, and it will fail with Blazor, as some of those options throw thw "Unsupported platform" exception.
80
+
81
+
Here is an example how to register the `RestClient` instance globally as a singleton:
In this case, the call will be made to a WebAPI server hosted at `http://localhost:5104/weather`. Remember that if the WebAPI server is not hosting the webassembly itself, it needs to have a CORS policy configured to allow the webassembly origin to access the API endpoint from the browser.
Copy file name to clipboardexpand all lines: docs/docs/usage/execute.md
+29
Original file line number
Diff line number
Diff line change
@@ -141,3 +141,32 @@ var statusCode = client.PostJsonAsync("orders", request, cancellationToken);
141
141
```
142
142
143
143
The same two extensions also exist for `PUT` requests (`PutJsonAsync`);
144
+
145
+
## Downloading binary data
146
+
147
+
There are two functions that allow you to download binary data from the remote API.
148
+
149
+
First, there's `DownloadDataAsync`, which returns `Task<byte[]`. It will read the binary response to the end, and return the whole binary content as a byte array. It works well for downloading smaller files.
150
+
151
+
For larger responses, you can use `DownloadStreamAsync` that returns `Task<Stream>`. This function allows you to open a stream reader and asynchronously stream large responses to memory or disk.
152
+
153
+
## JSON streaming
154
+
155
+
For HTTP API endpoints that stream the response data (like [Twitter search stream](https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream)) you can use RestSharp with `StreamJsonAsync<T>`, which returns an `IAsyncEnumerable<T>`:
The main limitation of this function is that it expects each JSON object to be returned as a single line. It is unable to parse the response by combining multiple lines into a JSON string.
Copy file name to clipboardexpand all lines: docs/docs/usage/request.md
+40
Original file line number
Diff line number
Diff line change
@@ -289,3 +289,43 @@ When you call `AddXmlBody`, it does the following for you:
289
289
Do not send XML string to `AddXmlBody`; it won't work!
290
290
:::
291
291
292
+
## Uploading files
293
+
294
+
To add a file to the request you can use the `RestRequest` function called `AddFile`. The main function accepts the `FileParameter` argument:
295
+
296
+
```csharp
297
+
request.AddFile(fileParameter);
298
+
```
299
+
300
+
You can instantiate the file parameter using `FileParameter.Create` that accepts a bytes array, or `FileParameter.FromFile`, which will load the file from disk.
301
+
302
+
There are also extension functions that wrap the creation of `FileParameter` inside:
0 commit comments