restsharp / RestSharp

Simple REST and HTTP API Client for .NET
https://restsharp.dev
Apache License 2.0
9.63k stars 2.34k forks source link

NullReferenceException in ExecutePost when Header value is null #2227

Closed TimSirmovics closed 4 months ago

TimSirmovics commented 5 months ago

Describe the bug When adding a header with a null value, an exception is thrown when sending the request and requires looking at the call stack to infer the cause of the exception. I think this could be made clearer by throwing the exception when adding the header, or returning a more detailed message in the later thrown exception.

To Reproduce

var client = new RestClient("baseUrl");

var request = new RestRequest("endpoint");
request.AddJsonBody(model);
request.AddHeader("X-SomeHeader", null);

var response = client.ExecutePost(request);

Expected behavior Either an exception is thrown on the AddHeader() call, or a more detailed exception message is returned when calling ExecutePost()

Stack trace

RestSharp.HttpRequestMessageExtensions.AddHeaders.__AddHeader|0_2(RestSharp.Parameter, System.Net.Http.Headers.HttpHeaders) RestSharp.HttpRequestMessageExtensions.AddHeaders.AnonymousMethod1(RestSharp.Parameter) RestSharp.HttpRequestMessageExtensions.AddHeaders(System.Net.Http.HttpRequestMessage, RestSharp.RequestHeaders) RestSharp.RestClient.ExecuteRequestAsync(RestSharp.RestRequest, System.Threading.CancellationToken) RestSharp.RestClient.ExecuteAsync(RestSharp.RestRequest, System.Threading.CancellationToken) RestSharp.AsyncHelpers.CustomSynchronizationContext.Run.PostCallback|7_0(object) RestSharp.AsyncHelpers.CustomSynchronizationContext.Run() RestSharp.AsyncHelpers.RunSync(System.Func) RestSharp.AsyncHelpers.RunSync(System.Func<System.Threading.Tasks.Task>) RestSharp.RestClientExtensions.ExecutePost(RestSharp.IRestClient, RestSharp.RestRequest)

Desktop (please complete the following information):