Skip to content

Commit 6a91263

Browse files
committed
Small docs update and multipart form content type format override
1 parent ee328c9 commit 6a91263

File tree

6 files changed

+361
-335
lines changed

6 files changed

+361
-335
lines changed

docs/usage.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,33 +298,33 @@ public async Task<RestResponse> ExecuteAsync(
298298
You can also avoid setting the request method upfront and use one of the overloads:
299299

300300
```csharp
301-
Task<RestResponse> ExecuteGetAsync
302-
Task<RestResponse> ExecutePostAsync
303-
Task<RestResponse> ExecutePutAsync
301+
Task<RestResponse> ExecuteGetAsync(RestRequest request, CancellationToken cancellationToken)
302+
Task<RestResponse> ExecutePostAsync(RestRequest request, CancellationToken cancellationToken)
303+
Task<RestResponse> ExecutePutAsync(RestRequest request, CancellationToken cancellationToken)
304304
```
305305

306306
When using any of those methods, you will get the response content as string in `response.Content`.
307307

308308
RestSharp can deserialize the response for you. To use that feature, use one of the generic overloads:
309309

310310
```csharp
311-
Task<RestResponse<T>> ExecuteAsync<T>
312-
Task<RestResponse<T>> ExecuteGetAsync<T>
313-
Task<RestResponse<T>> ExecutePostAsync<T>
314-
Task<RestResponse<T>> ExecutePutAsync<T>
311+
Task<RestResponse<T>> ExecuteAsync<T>(RestRequest request, CancellationToken cancellationToken)
312+
Task<RestResponse<T>> ExecuteGetAsync<T>(RestRequest request, CancellationToken cancellationToken)
313+
Task<RestResponse<T>> ExecutePostAsync<T>(RestRequest request, CancellationToken cancellationToken)
314+
Task<RestResponse<T>> ExecutePutAsync<T>(RestRequest request, CancellationToken cancellationToken)
315315
```
316316

317317
All the overloads that return `RestResponse` or `RestResponse<T>` don't throw an exception if the server returns an error. Read more about it [here](error-handling.md).
318318

319319
If you just need a deserialized response, you can use one of the extensions:
320320

321321
```csharp
322-
Task<T> GetAsync<T>
323-
Task<T> PostAsync<T>
324-
Task<T> PutAsync<T>
325-
Task<T> HeadAsync<T>
326-
Task<T> PatchAsync<T>
327-
Task<T> DeleteAsync<T>
322+
Task<T> GetAsync<T>(RestRequest request, CancellationToken cancellationToken)
323+
Task<T> PostAsync<T>(RestRequest request, CancellationToken cancellationToken)
324+
Task<T> PutAsync<T>(RestRequest request, CancellationToken cancellationToken)
325+
Task<T> HeadAsync<T>(RestRequest request, CancellationToken cancellationToken)
326+
Task<T> PatchAsync<T>(RestRequest request, CancellationToken cancellationToken)
327+
Task<T> DeleteAsync<T>(RestRequest request, CancellationToken cancellationToken)
328328
```
329329

330330
Those extensions will throw an exception if the server returns an error, as there's no other way to float the error back to the caller.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"devDependencies": {
3-
"vuepress-vite": "2.0.0-beta.27"
3+
"vuepress-vite": "^2.0.0-beta.33"
44
},
55
"scripts": {
66
"docs:dev": "vuepress dev docs",

src/RestSharp/Request/HttpContentExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static class HttpContentExtensions {
2222

2323
public static string GetFormBoundary(this MultipartFormDataContent content) {
2424
return GetBoundary(content);
25-
var contentType = content.Headers.ContentType?.ToString();
26-
var index = contentType?.IndexOf("boundary=", StringComparison.Ordinal) ?? 0;
27-
return index > 0 ? GetFormBoundary(contentType!, index) : "";
25+
// var contentType = content.Headers.ContentType?.ToString();
26+
// var index = contentType?.IndexOf("boundary=", StringComparison.Ordinal) ?? 0;
27+
// return index > 0 ? GetFormBoundary(contentType!, index) : "";
2828
}
2929

3030
static string GetFormBoundary(string headerValue, int index) {

src/RestSharp/Request/RequestContent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void AddHeader(Parameter parameter) {
187187

188188
string GetContentTypeHeader(string contentType)
189189
=> Content is MultipartFormDataContent mpContent
190-
? $"{contentType}; boundary=\"{mpContent.GetFormBoundary()}\""
190+
? _request.FormatMultipartContentType(contentType, mpContent.GetFormBoundary())
191191
: contentType;
192192

193193
public void Dispose() {

src/RestSharp/Request/RestRequest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace RestSharp;
1919

20+
public delegate string FormatContentTypeHeader(string contentType, string formBoundary);
21+
2022
/// <summary>
2123
/// Container for data used to make requests
2224
/// </summary>
@@ -145,6 +147,11 @@ public RestRequest(Uri resource, Method method = Method.Get)
145147
/// </summary>
146148
public HttpCompletionOption CompletionOption { get; set; } = HttpCompletionOption.ResponseContentRead;
147149

150+
/// <summary>
151+
/// Function that formats the content type header for multipart form fata
152+
/// </summary>
153+
public FormatContentTypeHeader FormatMultipartContentType { get; set; } = (contentType, boundary) => $"{contentType}; boundary=\"{boundary}\"";
154+
148155
/// <summary>
149156
/// Set this to write response to Stream rather than reading into memory.
150157
/// </summary>

0 commit comments

Comments
 (0)