Skip to content

Commit 29bf7a8

Browse files
committed
Allow parameter values to be null #1420
1 parent 9074a81 commit 29bf7a8

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

src/RestSharp/HttpHeader.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System;
1516
using JetBrains.Annotations;
1617

1718
namespace RestSharp
@@ -30,12 +31,20 @@ public class HttpHeader
3031
public HttpHeader(string name, string value)
3132
{
3233
Name = name;
33-
Value = value;
34+
Value = value ?? "";
3435
}
3536

37+
/// <summary>
38+
/// Creates a new instance of HttpHeader with value conversion
39+
/// </summary>
40+
/// <param name="name">Header name</param>
41+
/// <param name="value">Header value, which has to implement ToString() properly</param>
42+
public HttpHeader(string name, object value) : this(name, value?.ToString()) { }
43+
3644
/// <summary>
3745
/// Creates a new instance of HttpHeader. Remember to assign properties!
3846
/// </summary>
47+
[Obsolete("Use parameterized constructor")]
3948
public HttpHeader() { }
4049

4150
/// <summary>
@@ -48,4 +57,4 @@ public HttpHeader() { }
4857
/// </summary>
4958
public string Value { get; set; }
5059
}
51-
}
60+
}

src/RestSharp/HttpParameter.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,43 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System;
16+
using JetBrains.Annotations;
17+
1518
namespace RestSharp
1619
{
1720
/// <summary>
1821
/// Representation of an HTTP parameter (QueryString or Form value)
1922
/// </summary>
23+
[PublicAPI]
2024
public class HttpParameter
2125
{
2226
/// <summary>
23-
/// Name of the parameter
27+
/// Creates a new instance of HttpParameter
28+
/// </summary>
29+
/// <param name="name">Header name</param>
30+
/// <param name="value">Header value</param>
31+
/// <param name="contentType">Parameter content type</param>
32+
public HttpParameter(string name, string value, string contentType = null)
33+
{
34+
Name = name;
35+
ContentType = contentType;
36+
Value = value ?? "";
37+
}
38+
39+
/// <summary>
40+
/// Creates a new instance of HttpParameter with value conversion
41+
/// </summary>
42+
/// <param name="name">Header name</param>
43+
/// <param name="value">Header value, which has to implement ToString() properly</param>
44+
/// <param name="contentType">Parameter content type</param>
45+
public HttpParameter(string name, object value, string contentType = null) : this(name, value?.ToString(), contentType) { }
46+
47+
[Obsolete("Use parameterized constructor")]
48+
public HttpParameter() { }
49+
50+
/// <summary>
51+
/// Name of the parameter
2452
/// </summary>
2553
public string Name { get; set; }
2654

@@ -34,4 +62,4 @@ public class HttpParameter
3462
/// </summary>
3563
public string ContentType { get; set; }
3664
}
37-
}
65+
}

src/RestSharp/RestClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,17 +504,17 @@ IHttp ConfigureHttp(IRestRequest request)
504504

505505
http.Headers = requestParameters
506506
.Where(p => p.Type == ParameterType.HttpHeader)
507-
.Select(p => new HttpHeader {Name = p.Name, Value = Convert.ToString(p.Value)})
507+
.Select(p => new HttpHeader(p.Name, p.Value))
508508
.ToList();
509509

510510
http.Cookies = requestParameters
511511
.Where(p => p.Type == ParameterType.Cookie)
512-
.Select(p => new HttpCookie {Name = p.Name, Value = Convert.ToString(p.Value)})
512+
.Select(p => new HttpCookie {Name = p.Name, Value = p.Value?.ToString() ?? ""})
513513
.ToList();
514514

515515
http.Parameters = requestParameters
516-
.Where(p => p.Type == ParameterType.GetOrPost && p.Value != null)
517-
.Select(p => new HttpParameter {Name = p.Name, Value = Convert.ToString(p.Value)})
516+
.Where(p => p.Type == ParameterType.GetOrPost)
517+
.Select(p => new HttpParameter(p.Name, p.Value))
518518
.ToList();
519519

520520
http.Files = request.Files.Select(

src/RestSharp/RestRequestExtensions.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ params ISerializer[] serializers
3333

3434
if (body.DataFormat == DataFormat.None)
3535
{
36-
request.Body = new RequestBody(body.ContentType, body.Name, body.Value);
37-
return;
36+
request.Body = new RequestBody(body.ContentType, body.Name, body.Value);
37+
return;
3838
}
3939

4040
var contentType = body.ContentType ?? ContentType.FromDataFormat[body.DataFormat];
@@ -43,7 +43,8 @@ params ISerializer[] serializers
4343
if (requestSerializer != null)
4444
{
4545
request.Body = new RequestBody(
46-
requestSerializer.ContentType, requestSerializer.ContentType,
46+
requestSerializer.ContentType,
47+
requestSerializer.ContentType,
4748
requestSerializer.Serialize(body.Value)
4849
);
4950
return;
@@ -78,15 +79,8 @@ internal static void AddBody(this IHttp http, RequestBody requestBody)
7879
}
7980
else
8081
{
81-
http.Parameters.Add(
82-
new HttpParameter
83-
{
84-
Name = requestBody.Name,
85-
Value = requestBody.Value.ToString(),
86-
ContentType = requestBody.ContentType
87-
}
88-
);
82+
http.Parameters.Add(new HttpParameter(requestBody.Name, requestBody.Value, requestBody.ContentType));
8983
}
9084
}
9185
}
92-
}
86+
}

test/RestSharp.IntegrationTests/HttpHeadersTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public void Ensure_headers_correctly_set_in_the_hook()
3535
// Prepare
3636
var client = new RestClient(server.Url);
3737

38-
var request = new RestRequest(RequestHeadCapturer.Resource);
39-
request.OnBeforeRequest = http => http.Headers.Add(new HttpHeader {Name = headerName, Value = headerValue});
38+
var request = new RestRequest(RequestHeadCapturer.Resource)
39+
{
40+
OnBeforeRequest = http => http.Headers.Add(new HttpHeader(headerName, headerValue))
41+
};
4042

4143
// Run
4244
client.Execute(request);

0 commit comments

Comments
 (0)