MelbourneDeveloper/RestClient.Net

Call modifies headers collection

MelbourneDeveloper opened this issue ยท 13 comments

When calling SendAsync with a collection of headers, the call can modify the collection that is sent in. This can cause errors in the subsequent calls. A new collection of headers need to be created and merged with default headers.

Something like this still happens there is a bug when you send the headers but it seam to happen only on release, I have been searching like crazy before I post an Issue but the but is still happening

if you execute the following in release you will get a duplicated key exception in the headers, is there a way to fix this? I can give you a hand and fix it myself if you point me in the right direction

var result = await client.PostAsync<DataResult, IDataParameters>(Parameters, resource, Headers);

@egarim ,

Firstly, I haven't confirmed that this is a bug. I have a feeling that this is not an issue anymore. I don't think the code is putting default http headers on the HttpClient. You should use Fiddler to check what headers are being sent to the server.

This is the code that I think is relevant:

Please let me know how you go

@egarim

you will get a duplicated key exception in the headers

Do you perhaps have duplicate keys in the headers somehow? Can you send a screenshot and stack trace of the error you see?

I will make a reproduction project I have been tracking this bug for 2 weeks

@egarim thankyou!

I'd you can point me to code that breaks, I will do my best to fix it.

If you can write a failing unit test, even better.

Hi Christian
I have new information on this topic, but I need to create a unit test to prove my theory, so what I found so far is that the error happens when you reuse the headers
for example

            var Headers = new RequestHeadersCollection();
          

            var result1 = await client.PostAsync<DataResult, IDataParameters>(Parameters, resource, Headers);
            var result2 = await client.PostAsync<DataResult, IDataParameters>(Parameters, resource, Headers);

I use a new instance of headers on each request the problem does not seem to happen, here my question is should headers be reusable or should I create a new instance every time I do a request?

@egarim thanks for the information. I might be able to look at this soon. So, you are saying that this code fails with an exception?

@MelbourneDeveloper a code like that will reproduce the issue, I'm reusing the headers but I really don't know if that's allowed, the exception does not happen if you create a new instance of the headers for each request

This sounds like a bug. If so, it should be easy enough to fix. Thankyou!

@egarim I can recreate the issue. Fix on its way.

You can see the failing unit test in this commit: 1a7065c#diff-e85b635cb1750ff7a20a4e42cc61e1a6R304

@egarim the fix in this nuget package

There are also some new unit tests to make sure this bug doesn't creep back, and to make sure that general header management is working correctly.

@MelbourneDeveloper Thanks Christian

@egarim does this resolve your issue?