1

Good Day peeps,

I have been stuck at this issue for around 6 hours and i can't figure out what's wrong with it.Firstly, i'm trying to send a Request to an server end point which requires some form of basic auth as json. But the when it hits the method "Execute" it throws an null reference exception. Not the code below:

        SomeObjectClass someObject = new SomeObjectClass();

        RestRequest request = new RestRequest();
        request.Method = Method.POST;
        request.JsonSerializer = new CustomJsonSerializer();
        request.RequestFormat = DataFormat.Json;

        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });

        var client = new RestClient();

        client.BaseUrl = "https://www.somewebsite.com/someapi/something";
        client.Authenticator = new HttpBasicAuthenticator("username","password");
        client.AddHandler("application/json", new CustomerSerializer);

        request.AddBody(someObject);

        var result = client.Execute<dynamic>(request);

        return result;

And i've been getting NullReferenceException :

at RestSharp.HttpBasicAuthenticator.<Authenticate>b__0(Parameter p)
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
at RestSharp.HttpBasicAuthenticator.Authenticate(IRestClient client, IRestRequest request)
at RestSharp.RestClient.AuthenticateIfNeeded(RestClient client, IRestRequest request)
at RestSharp.RestClient.Execute(IRestRequest request, String httpMethod, Func`3 getResponse)
at RestSharp.RestClient.Execute(IRestRequest request)
at RestSharp.RestClient.Execute[T](IRestRequest request)

My initial guess will be my lack of understanding of ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); caused this problem, but i can't really pin point it. I've been spending quite some time on this issue and resort to posting the problem, it'll be really appreciated if some expert can provide an answer on this.

2
  • Which line of code is throwing the exception? Commented Dec 12, 2013 at 16:17
  • @CharlieKilian client.Execute<dynamic>(request) is throwing that error Commented Dec 12, 2013 at 16:36

1 Answer 1

2

The solution for me was adding a parameter name (I only have one parameter).

My code before the change:

//...set up client and create new request...
request.AddParameter(new Parameter()
{
  //Name was not included and therefore null
  Type = ParameterType.RequestBody,
  Value = JsonConvert.SerializeObject(new
  {
    name,
    domains,
    custom_fields = customFields
  })
});

Line 42 of HttpBasicAuthenticator is where the exception was being thrown because a null value for Name is not being handled.

if (!request.Parameters.Any(p => p.Name.Equals("Authorization", StringComparison.OrdinalIgnoreCase)))

I changed my code to the following and it worked:

request.AddParameter(new Parameter()
{
  Name = "",
  Type = ParameterType.RequestBody,
  Value = JsonConvert.SerializeObject(new
  {
    name,
    domains,
    custom_fields = customFields
  })
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.