Skip to content

Commit cbee3c1

Browse files
author
Michael Hallett
committed
Fixed a possible null reference exception on parameters when ToString was being called on the value; Code cleanup;
1 parent 629ea54 commit cbee3c1

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

RestSharp/RestClient.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public RestClient()
6666
/// Sets the BaseUrl property for requests made by this client instance
6767
/// </summary>
6868
/// <param name="baseUrl"></param>
69-
public RestClient(string baseUrl)
70-
: this()
69+
public RestClient(string baseUrl) : this()
7170
{
7271
BaseUrl = baseUrl;
7372
}
@@ -130,6 +129,9 @@ public void ClearHandlers()
130129
/// <returns>IDeserializer instance</returns>
131130
private IDeserializer GetHandler(string contentType)
132131
{
132+
if (contentType == null)
133+
throw new ArgumentNullException("contentType");
134+
133135
if (string.IsNullOrEmpty(contentType) && ContentHandlers.ContainsKey("*"))
134136
{
135137
return ContentHandlers["*"];
@@ -391,7 +393,7 @@ private void ConfigureHttp(IRestRequest request, IHttp http)
391393
select new HttpHeader
392394
{
393395
Name = p.Name,
394-
Value = p.Value.ToString()
396+
Value = Convert.ToString(p.Value)
395397
};
396398

397399
foreach (var header in headers)
@@ -404,7 +406,7 @@ private void ConfigureHttp(IRestRequest request, IHttp http)
404406
select new HttpCookie
405407
{
406408
Name = p.Name,
407-
Value = p.Value.ToString()
409+
Value = Convert.ToString(p.Value)
408410
};
409411

410412
foreach (var cookie in cookies)
@@ -418,7 +420,7 @@ private void ConfigureHttp(IRestRequest request, IHttp http)
418420
select new HttpParameter
419421
{
420422
Name = p.Name,
421-
Value = p.Value.ToString()
423+
Value = Convert.ToString(p.Value)
422424
};
423425

424426
foreach (var parameter in @params)
@@ -451,17 +453,18 @@ private void ConfigureHttp(IRestRequest request, IHttp http)
451453
if (!http.Files.Any())
452454
{
453455
object val = body.Value;
456+
454457
if (val is byte[])
455458
http.RequestBodyBytes = (byte[]) val;
456459
else
457-
http.RequestBody = body.Value.ToString();
460+
http.RequestBody = Convert.ToString(body.Value);
458461
}
459462
else
460463
{
461464
http.Parameters.Add(new HttpParameter
462465
{
463466
Name = body.Name,
464-
Value = body.Value.ToString()
467+
Value = Convert.ToString(body.Value)
465468
});
466469
}
467470
}
@@ -482,20 +485,22 @@ private void ConfigureProxy(IHttp http)
482485

483486
private RestResponse ConvertToRestResponse(IRestRequest request, HttpResponse httpResponse)
484487
{
485-
var restResponse = new RestResponse();
486-
restResponse.Content = httpResponse.Content;
487-
restResponse.ContentEncoding = httpResponse.ContentEncoding;
488-
restResponse.ContentLength = httpResponse.ContentLength;
489-
restResponse.ContentType = httpResponse.ContentType;
490-
restResponse.ErrorException = httpResponse.ErrorException;
491-
restResponse.ErrorMessage = httpResponse.ErrorMessage;
492-
restResponse.RawBytes = httpResponse.RawBytes;
493-
restResponse.ResponseStatus = httpResponse.ResponseStatus;
494-
restResponse.ResponseUri = httpResponse.ResponseUri;
495-
restResponse.Server = httpResponse.Server;
496-
restResponse.StatusCode = httpResponse.StatusCode;
497-
restResponse.StatusDescription = httpResponse.StatusDescription;
498-
restResponse.Request = request;
488+
var restResponse = new RestResponse
489+
{
490+
Content = httpResponse.Content,
491+
ContentEncoding = httpResponse.ContentEncoding,
492+
ContentLength = httpResponse.ContentLength,
493+
ContentType = httpResponse.ContentType,
494+
ErrorException = httpResponse.ErrorException,
495+
ErrorMessage = httpResponse.ErrorMessage,
496+
RawBytes = httpResponse.RawBytes,
497+
ResponseStatus = httpResponse.ResponseStatus,
498+
ResponseUri = httpResponse.ResponseUri,
499+
Server = httpResponse.Server,
500+
StatusCode = httpResponse.StatusCode,
501+
StatusDescription = httpResponse.StatusDescription,
502+
Request = request
503+
};
499504

500505
foreach (var header in httpResponse.Headers)
501506
{
@@ -547,6 +552,7 @@ private IRestResponse<T> Deserialize<T>(IRestRequest request, IRestResponse raw)
547552
if (response.ErrorException == null)
548553
{
549554
IDeserializer handler = GetHandler(raw.ContentType);
555+
550556
// Only continue if there is a handler defined else there is no way to deserialize the data.
551557
// This can happen when a request returns for example a 404 page instead of the requested JSON/XML resource
552558
if (handler != null)

0 commit comments

Comments
 (0)