Skip to content

Commit e4032ba

Browse files
committed
Add content headers to the response for #1684
1 parent 949a342 commit e4032ba

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright © 2009-2020 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
using System.Net.Http.Headers;
17+
18+
namespace RestSharp.Extensions;
19+
20+
static class HttpHeadersExtensions {
21+
public static IReadOnlyCollection<HeaderParameter> GetHeaderParameters(this HttpHeaders httpHeaders)
22+
=> httpHeaders
23+
.SelectMany(x => x.Value.Select(y => (x.Key, y)))
24+
.Select(x => new HeaderParameter(x.Key, x.y))
25+
.ToList();
26+
}

src/RestSharp/Response/RestResponse.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
using System.Text;
1919
using RestSharp.Extensions;
2020

21+
// ReSharper disable SuggestBaseTypeForParameter
22+
2123
namespace RestSharp;
2224

2325
/// <summary>
@@ -42,6 +44,7 @@ public static RestResponse<T> FromResponse(RestResponse response)
4244
ErrorMessage = response.ErrorMessage,
4345
ErrorException = response.ErrorException,
4446
Headers = response.Headers,
47+
ContentHeaders = response.ContentHeaders,
4548
IsSuccessful = response.IsSuccessful,
4649
ResponseStatus = response.ResponseStatus,
4750
ResponseUri = response.ResponseUri,
@@ -57,13 +60,9 @@ public static RestResponse<T> FromResponse(RestResponse response)
5760
/// </summary>
5861
[DebuggerDisplay("{" + nameof(DebuggerDisplay) + "()}")]
5962
public class RestResponse : RestResponseBase {
60-
RestResponse SetHeaders(HttpResponseHeaders headers) {
61-
var headerParams = headers
62-
.SelectMany(x => x.Value.Select(y => (x.Key, y)))
63-
.Select(x => new HeaderParameter(x.Key, x.y))
64-
.ToList();
65-
return this.With(x => x.Headers = headerParams);
66-
}
63+
RestResponse SetHeaders(HttpResponseHeaders httpHeaders) => this.With(x => x.Headers = httpHeaders.GetHeaderParameters());
64+
65+
RestResponse SetContentHeaders(HttpContentHeaders httpHeaders) => this.With(x => x.ContentHeaders = httpHeaders.GetHeaderParameters());
6766

6867
RestResponse SetCookies(CookieCollection cookies) => this.With(x => x.Cookies = cookies);
6968

@@ -124,6 +123,7 @@ async Task<RestResponse> GetDefaultResponse() {
124123
Request = request
125124
}
126125
.SetHeaders(httpResponse.Headers)
126+
.SetContentHeaders(httpResponse.Content.Headers)
127127
.SetCookies(cookieCollection);
128128
}
129129
}

src/RestSharp/Response/RestResponseBase.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,14 @@ public abstract class RestResponseBase {
9191
public CookieCollection? Cookies { get; protected internal set; }
9292

9393
/// <summary>
94-
/// Headers returned by server with the response
94+
/// Response headers returned by server with the response
9595
/// </summary>
96-
public IList<HeaderParameter>? Headers { get; protected internal set; }
96+
public IReadOnlyCollection<HeaderParameter>? Headers { get; protected internal set; }
97+
98+
/// <summary>
99+
/// Content headers returned by server with the response
100+
/// </summary>
101+
public IReadOnlyCollection<HeaderParameter>? ContentHeaders { get; protected internal set; }
97102

98103
/// <summary>
99104
/// Status of the request. Will return Error for transport errors.

0 commit comments

Comments
 (0)