Skip to content

Commit 6dc5f1a

Browse files
committed
Prevent content headers from being added when there's no body
1 parent 1ffa419 commit 6dc5f1a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/RestSharp/Request/RequestContent.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,14 @@ void AddPostParameters() {
138138
}
139139

140140
void AddHeaders() {
141-
_request.Parameters
141+
var contentHeaders = _request.Parameters
142142
.Where(x => x.Type == ParameterType.HttpHeader && ContentHeaders.Contains(x.Name))
143-
.ForEach(AddHeader);
143+
.ToArray();
144+
145+
if (contentHeaders.Length > 0 && Content == null)
146+
throw new InvalidRequestException("Content headers should not be used when there's no body in the request");
147+
148+
contentHeaders.ForEach(AddHeader);
144149

145150
void AddHeader(Parameter parameter) {
146151
var parameterStringValue = parameter.Value!.ToString();
@@ -156,10 +161,7 @@ void AddHeader(Parameter parameter) {
156161
}
157162

158163
string GetContentTypeHeader(string contentType) {
159-
if (Content == null)
160-
throw new InvalidRequestException("Content type headers should not be used when there's no body in the request");
161-
162-
var boundary = Content.GetFormBoundary();
164+
var boundary = Content!.GetFormBoundary();
163165
return boundary.IsEmpty() ? contentType : $"{contentType}; boundary=\"{boundary}\"";
164166
}
165167

0 commit comments

Comments
 (0)