refactor(ProxyController): Updated to add default headers

This commit is contained in:
Developer 02 2025-01-30 12:10:30 +01:00
parent d8ee61d107
commit 61bb5acdff
3 changed files with 11 additions and 6 deletions

View File

@ -4,6 +4,6 @@
{
public required string Url { get; init; }
public Dictionary<string, string>? DefaultHeaders { get; init; }
public Dictionary<string, string> DefaultHeaders { get; init; } = new();
}
}

View File

@ -11,12 +11,12 @@ using System.Threading.Tasks;
[ApiController]
public class ProxyController(IOptions<OriginServerParams> originServerParamsOptions) : ControllerBase
{
private readonly OriginServerParams origin = originServerParamsOptions.Value;
private readonly OriginServerParams _origin = originServerParamsOptions.Value;
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
public async Task<IActionResult> Redir([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company)
{
var res = await origin.Url
var res = await _origin.Url
.AppendPathSegment(servicetierName)
.AppendPathSegment("ODataV4")
.AppendPathSegment(webserviceName + "_CreateInvoice")
@ -24,9 +24,14 @@ public class ProxyController(IOptions<OriginServerParams> originServerParamsOpti
//.WithOAuthBearerToken("my_oauth_token")
.PostAsync();
//merge with default headers
var mergedDictionary = _origin.DefaultHeaders
.Concat(res.Headers.ToDictionary(kv => kv.Name, kv => kv.Value))
.ToDictionary(kv => kv.Key, kv => kv.Value);
// set headers
foreach (var (Name, Value) in res.Headers)
HttpContext.Response.Headers[Name] = Value;
foreach (var header in mergedDictionary)
HttpContext.Response.Headers[header.Key] = header.Value;
// set cookies
foreach (var (Name, Value) in res.Cookies.ToKeyValuePairs())

View File

@ -9,7 +9,7 @@
"OriginServer": {
"Url": "https://localhost:7248",
"DefaultHeaders": {
"foo": "bar"
}
}
}