Skip to content
This repository was archived by the owner on Aug 18, 2021. It is now read-only.
This repository was archived by the owner on Aug 18, 2021. It is now read-only.

LightApi won't correctly set headers for requests #66

Open
@guilhermeluizsp

Description

@guilhermeluizsp

Each request method from LightAPI accepts a parameter header, which is used to populate the Headers of the HTTP request.

https://github.com/Avanade/Liquid-Application-Framework/blob/93f7874f2ead13d3602cc39fc1e9355206217f57/src/Liquid.Domain/API/LightAPI.cs#L62

https://github.com/Avanade/Liquid-Application-Framework/blob/93f7874f2ead13d3602cc39fc1e9355206217f57/src/Liquid.Domain/API/LightAPI.cs#L106

https://github.com/Avanade/Liquid-Application-Framework/blob/93f7874f2ead13d3602cc39fc1e9355206217f57/src/Liquid.Domain/API/LightAPI.cs#L148

https://github.com/Avanade/Liquid-Application-Framework/blob/93f7874f2ead13d3602cc39fc1e9355206217f57/src/Liquid.Domain/API/LightAPI.cs#L173

Those headers should only be sent with that specific request, but looking at the implementation we can see that it overrides the DefaultRequestHeaders of the underlying HttpClient:

https://github.com/Avanade/Liquid-Application-Framework/blob/93f7874f2ead13d3602cc39fc1e9355206217f57/src/Liquid.Domain/API/LightAPI.cs#L76-L80

(this is a code snippet from the Get method, but this logic repeats over every other method inside LightAPI)

This is problematic since headers added to that property will be sent to every request.

Also, if we start considering multi threaded scenarios, we may face race conditions (take #65 as an example. If two or more requests override the Authentication Header at the same time, we may face unexpected 401 (unauthorized) statuses since the wrong token would have been sent).

To fix this, we should instantiate a new HttpRequestMessage, override its Headers property and use the SendAsync method. That will created request-scoped headers.

var httpRequestMessage = new HttpRequestMessage
{
    Method = /*...*/,
    RequestUri = /*...*/,
    Headers = { 
        { "Header name", "Header value" },
    },
    Content = /*...*/
};

httpClient.SendAsync(httpRequestMessage)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcomplexity:lowThe complexity of the issue is lowpriorityThis issue should be acted upon before other issues.size:smallThe size of the issue is small

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions