From 182b3dcdaf3aa246bd2aebd44c2ac19ee140a964 Mon Sep 17 00:00:00 2001 From: "Jan Ivar Z. Carlsen" Date: Wed, 24 Jul 2024 11:00:05 +0200 Subject: [PATCH] Added extensions for getting content header values --- .../Response/RestResponseExtensions.cs | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/RestSharp/Response/RestResponseExtensions.cs b/src/RestSharp/Response/RestResponseExtensions.cs index 4a039ed7b..32735f571 100644 --- a/src/RestSharp/Response/RestResponseExtensions.cs +++ b/src/RestSharp/Response/RestResponseExtensions.cs @@ -22,7 +22,7 @@ public static class RestResponseExtensions { /// Name of the header /// Header value or null if the header is not found in the response public static string? GetHeaderValue(this RestResponse response, string headerName) - => response.Headers?.FirstOrDefault(x => NameIs(x.Name, headerName))?.Value?.ToString(); + => response.Headers?.FirstOrDefault(x => NameIs(x.Name, headerName))?.Value.ToString(); /// /// Gets all the values of the header with the specified name. @@ -33,7 +33,29 @@ public static class RestResponseExtensions { public static string[] GetHeaderValues(this RestResponse response, string headerName) => response.Headers ?.Where(x => NameIs(x.Name, headerName)) - .Select(x => x.Value?.ToString() ?? "") + .Select(x => x.Value.ToString() ?? "") + .ToArray() ?? + []; + + /// + /// Gets the value of the content header with the specified name. + /// + /// Response object + /// Name of the header + /// Header value or null if the content header is not found in the response + public static string? GetContentHeaderValue(this RestResponse response, string headerName) + => response.ContentHeaders?.FirstOrDefault(x => NameIs(x.Name, headerName))?.Value.ToString(); + + /// + /// Gets all the values of the content header with the specified name. + /// + /// Response object + /// Name of the header + /// Array of header values or empty array if the content header is not found in the response + public static string[] GetContentHeaderValues(this RestResponse response, string headerName) + => response.ContentHeaders + ?.Where(x => NameIs(x.Name, headerName)) + .Select(x => x.Value.ToString() ?? "") .ToArray() ?? [];