Skip to content

NullReferenceException in ExecutePost when Header value is null #2227

Closed
@TimSirmovics

Description

@TimSirmovics

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.AnonymousMethod__1(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<System.Threading.Tasks.Task>)
RestSharp.AsyncHelpers.RunSync(System.Func<System.Threading.Tasks.Task>)
RestSharp.RestClientExtensions.ExecutePost(RestSharp.IRestClient, RestSharp.RestRequest)

Desktop (please complete the following information):

  • OS: Windows 11
  • .NET version: .NET 8.0
  • Version 111.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions