18
18
using RestSharp . Serialization ;
19
19
using RestSharp . Serializers ;
20
20
21
- namespace RestSharp
22
- {
23
- internal static class RestRequestExtensions
24
- {
21
+ namespace RestSharp {
22
+ internal static class RestRequestExtensions {
25
23
internal static void SerializeRequestBody (
26
- this IRestRequest request ,
24
+ this IRestRequest request ,
27
25
IDictionary < DataFormat , IRestSerializer > restSerializers ,
28
- params ISerializer [ ] serializers
29
- )
30
- {
26
+ params ISerializer [ ] serializers
27
+ ) {
31
28
var body = request . Parameters . FirstOrDefault ( p => p . Type == ParameterType . RequestBody ) ;
32
29
if ( body == null ) return ;
33
30
34
- if ( body . DataFormat == DataFormat . None )
35
- {
31
+ if ( body . DataFormat == DataFormat . None ) {
36
32
request . Body = new RequestBody ( body . ContentType , body . Name , body . Value ) ;
37
33
return ;
38
34
}
39
35
40
36
var contentType = body . ContentType ?? ContentType . FromDataFormat [ body . DataFormat ] ;
41
37
var requestSerializer = serializers . FirstOrDefault ( x => x != null && x . ContentType == contentType ) ;
42
38
43
- if ( requestSerializer != null )
44
- {
39
+ if ( requestSerializer != null ) {
45
40
request . Body = new RequestBody (
46
41
requestSerializer . ContentType ,
47
42
requestSerializer . ContentType ,
@@ -52,14 +47,13 @@ params ISerializer[] serializers
52
47
53
48
if ( ! restSerializers . TryGetValue ( body . DataFormat , out var serializer ) )
54
49
throw new InvalidDataContractException (
55
- $ "Can't find serializer for content type { body . DataFormat } "
50
+ $ "Can't find serializer for data type { body . DataFormat } "
56
51
) ;
57
52
58
- request . Body = new RequestBody ( serializer . ContentType , serializer . ContentType , serializer . Serialize ( body ) ) ;
53
+ request . Body = new RequestBody ( body . ContentType ?? serializer . ContentType , serializer . ContentType , serializer . Serialize ( body ) ! ) ;
59
54
}
60
55
61
- internal static void AddBody ( this IHttp http , RequestBody requestBody )
62
- {
56
+ internal static void AddBody ( this IHttp http , RequestBody requestBody ) {
63
57
// Only add the body if there aren't any files to make it a multipart form request
64
58
// If there are files or AlwaysMultipartFormData = true, then add the body to the HTTP Parameters
65
59
if ( requestBody . Value == null ) return ;
@@ -68,19 +62,17 @@ internal static void AddBody(this IHttp http, RequestBody requestBody)
68
62
? requestBody . ContentType
69
63
: requestBody . Name ;
70
64
71
- if ( ! http . AlwaysMultipartFormData && ! http . Files . Any ( ) )
72
- {
65
+ if ( ! http . AlwaysMultipartFormData && ! http . Files . Any ( ) ) {
73
66
var val = requestBody . Value ;
74
67
75
68
if ( val is byte [ ] bytes )
76
69
http . RequestBodyBytes = bytes ;
77
70
else
78
71
http . RequestBody = requestBody . Value . ToString ( ) ;
79
72
}
80
- else
81
- {
73
+ else {
82
74
http . Parameters . Add ( new HttpParameter ( requestBody . Name , requestBody . Value , requestBody . ContentType ) ) ;
83
75
}
84
76
}
85
77
}
86
- }
78
+ }
0 commit comments