1818using RestSharp . Serialization ;
1919using RestSharp . Serializers ;
2020
21- namespace RestSharp
22- {
23- internal static class RestRequestExtensions
24- {
21+ namespace RestSharp {
22+ internal static class RestRequestExtensions {
2523 internal static void SerializeRequestBody (
26- this IRestRequest request ,
24+ this IRestRequest request ,
2725 IDictionary < DataFormat , IRestSerializer > restSerializers ,
28- params ISerializer [ ] serializers
29- )
30- {
26+ params ISerializer [ ] serializers
27+ ) {
3128 var body = request . Parameters . FirstOrDefault ( p => p . Type == ParameterType . RequestBody ) ;
3229 if ( body == null ) return ;
3330
34- if ( body . DataFormat == DataFormat . None )
35- {
31+ if ( body . DataFormat == DataFormat . None ) {
3632 request . Body = new RequestBody ( body . ContentType , body . Name , body . Value ) ;
3733 return ;
3834 }
3935
4036 var contentType = body . ContentType ?? ContentType . FromDataFormat [ body . DataFormat ] ;
4137 var requestSerializer = serializers . FirstOrDefault ( x => x != null && x . ContentType == contentType ) ;
4238
43- if ( requestSerializer != null )
44- {
39+ if ( requestSerializer != null ) {
4540 request . Body = new RequestBody (
4641 requestSerializer . ContentType ,
4742 requestSerializer . ContentType ,
@@ -52,14 +47,13 @@ params ISerializer[] serializers
5247
5348 if ( ! restSerializers . TryGetValue ( body . DataFormat , out var serializer ) )
5449 throw new InvalidDataContractException (
55- $ "Can't find serializer for content type { body . DataFormat } "
50+ $ "Can't find serializer for data type { body . DataFormat } "
5651 ) ;
5752
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 ) ! ) ;
5954 }
6055
61- internal static void AddBody ( this IHttp http , RequestBody requestBody )
62- {
56+ internal static void AddBody ( this IHttp http , RequestBody requestBody ) {
6357 // Only add the body if there aren't any files to make it a multipart form request
6458 // If there are files or AlwaysMultipartFormData = true, then add the body to the HTTP Parameters
6559 if ( requestBody . Value == null ) return ;
@@ -68,19 +62,17 @@ internal static void AddBody(this IHttp http, RequestBody requestBody)
6862 ? requestBody . ContentType
6963 : requestBody . Name ;
7064
71- if ( ! http . AlwaysMultipartFormData && ! http . Files . Any ( ) )
72- {
65+ if ( ! http . AlwaysMultipartFormData && ! http . Files . Any ( ) ) {
7366 var val = requestBody . Value ;
7467
7568 if ( val is byte [ ] bytes )
7669 http . RequestBodyBytes = bytes ;
7770 else
7871 http . RequestBody = requestBody . Value . ToString ( ) ;
7972 }
80- else
81- {
73+ else {
8274 http . Parameters . Add ( new HttpParameter ( requestBody . Name , requestBody . Value , requestBody . ContentType ) ) ;
8375 }
8476 }
8577 }
86- }
78+ }
0 commit comments