Skip to content

Commit

Permalink
Added extensions for getting content header values (#2247)
Browse files Browse the repository at this point in the history
  • Loading branch information
jizc committed Jul 30, 2024
1 parent 75ccc00 commit 6391624
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/RestSharp/Response/RestResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class RestResponseExtensions {
/// <param name="headerName">Name of the header</param>
/// <returns>Header value or null if the header is not found in the response</returns>
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();

/// <summary>
/// Gets all the values of the header with the specified name.
Expand All @@ -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() ??
[];

/// <summary>
/// Gets the value of the content header with the specified name.
/// </summary>
/// <param name="response">Response object</param>
/// <param name="headerName">Name of the header</param>
/// <returns>Header value or null if the content header is not found in the response</returns>
public static string? GetContentHeaderValue(this RestResponse response, string headerName)
=> response.ContentHeaders?.FirstOrDefault(x => NameIs(x.Name, headerName))?.Value.ToString();

/// <summary>
/// Gets all the values of the content header with the specified name.
/// </summary>
/// <param name="response">Response object</param>
/// <param name="headerName">Name of the header</param>
/// <returns>Array of header values or empty array if the content header is not found in the response</returns>
public static string[] GetContentHeaderValues(this RestResponse response, string headerName)
=> response.ContentHeaders
?.Where(x => NameIs(x.Name, headerName))
.Select(x => x.Value.ToString() ?? "")
.ToArray() ??
[];

Expand Down

0 comments on commit 6391624

Please sign in to comment.