Flurl: NullReferenceException when setting a content-type header but not actually sending content

Created on 30 Apr 2018  路  8Comments  路  Source: tmenier/Flurl

I have a strange issue. All Post requests are working fine but any variant of GET requests are failing with below error

Exception- System.NullReferenceException: Object reference not set to an instance of an object.Exception- System.NullReferenceException: Object reference not set to an instance of an object.
  at Flurl.Http.FlurlRequest.WriteHeaders (System.Net.Http.HttpRequestMessage request) [0x0013c] in <0b8ec929b0304612945586497daf7e3c>:0 
  at Flurl.Http.FlurlRequest+<SendAsync>d__19.MoveNext () [0x001cc] in <0b8ec929b0304612945586497daf7e3c>:0 

I'm re-using flurlclient across all post & get requests since its calling to same domain. Pls help me out how to find original issue.
I should add that this is happening after updating to latest Nuget.

bug

Most helpful comment

Great. You probably set it because the API you're calling requires it, or just that it's a best practice for sending any JSON request body. Flurl doing it for you is sort of a subtle feature. :)

This is still a bug in general though. Thanks for reporting.

All 8 comments

Most likely it's a regression bug related to #256. But I need a little more info. Can you post the code where you create/configure your FlurlClient, as well as the specific GET call? I'm looking for anything that might directly or indirectly affect the request headers. Thanks.

I'm injecting flurlcient as singleton and configuring as below

_flurlClient = flurlClientFac.Get(rootUrl)
           .WithHeader(APIHeader, ApiKey)
          .WithHeader(SessionHeader, SessionToken)
           .WithHeader("Content-Type", "application/json");

GET request
var result = await _flurlClient.Request(ApiUrl + "/getEmpData").GetJsonAsync();

What are APIHeader and SessionHeader? I'm guessing one of them is null.

They are just string constants and not null. Request headers are set correctly as validated during debugging and its working with POST requests.

Ah! I think I see what's happening. You're setting a content-type but not sending any content. My guess is you're doing that so you can reuse this when you _are_ sending content (i.e. POSTs), so I should definitely add a null check in Flurl.

However, if you're using Flurl's PostJsonAsync for your POST requests, content-type is set to application/json automatically, so I think if you just remove the line that sets that, all should be good.

Thanks. removing content-type header works. I don't know why did it I set it explicitly.

Great. You probably set it because the API you're calling requires it, or just that it's a best practice for sending any JSON request body. Flurl doing it for you is sort of a subtle feature. :)

This is still a bug in general though. Thanks for reporting.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tmenier picture tmenier  路  4Comments

michael-wolfenden picture michael-wolfenden  路  3Comments

dagostinelli picture dagostinelli  路  5Comments

bchavez picture bchavez  路  3Comments

Aw3same picture Aw3same  路  6Comments