Skip to content

Commit fbd9b05

Browse files
committed
Merge branch 'uxsoft-contenttype-overwritten-by-body-type' into develop
2 parents ef2744c + 4d73208 commit fbd9b05

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

RestSharp.IntegrationTests/StatusCodeTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,35 @@ public void Reports_5xx_Status_Code_Success_Accurately()
141141

142142
Assert.IsFalse(response.IsSuccessful);
143143
}
144+
145+
[Test]
146+
public void ContentType_Additional_Information()
147+
{
148+
_server.SetHandler(Handlers.Generic<ResponseHandler>());
149+
var request = new RestRequest(Method.POST)
150+
{
151+
RequestFormat = DataFormat.Json,
152+
Resource = "contenttype_odata"
153+
};
154+
request.AddBody("bodyadsodajjd");
155+
request.AddHeader("X-RequestDigest", "xrequestdigestasdasd");
156+
request.AddHeader("Accept", "application/json; odata=verbose");
157+
request.AddHeader("Content-Type", "application/json; odata=verbose");
158+
159+
IRestResponse<Response> response = _client.Execute<Response>(request);
160+
161+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
162+
}
144163
}
145164

146165
public class ResponseHandler
147166
{
167+
private void contenttype_odata(HttpListenerContext context)
168+
{
169+
bool hasCorrectHeader = context.Request.Headers["Content-Type"] == "application/json; odata=verbose";
170+
context.Response.StatusCode = hasCorrectHeader ? 200 : 400;
171+
}
172+
148173
private void error(HttpListenerContext context)
149174
{
150175
context.Response.StatusCode = 400;

RestSharp/Http.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,18 +316,23 @@ private string EncodeParameters()
316316

317317
private void PreparePostBody(HttpWebRequest webRequest)
318318
{
319+
bool needsContentType = String.IsNullOrEmpty(webRequest.ContentType);
320+
319321
if (HasFiles || AlwaysMultipartFormData)
320322
{
321-
webRequest.ContentType = GetMultipartFormContentType();
323+
if (needsContentType)
324+
webRequest.ContentType = GetMultipartFormContentType();
322325
}
323326
else if (HasParameters)
324327
{
325-
webRequest.ContentType = "application/x-www-form-urlencoded";
328+
if (needsContentType)
329+
webRequest.ContentType = "application/x-www-form-urlencoded";
326330
RequestBody = EncodeParameters();
327331
}
328332
else if (HasBody)
329333
{
330-
webRequest.ContentType = RequestContentType;
334+
if (needsContentType)
335+
webRequest.ContentType = RequestContentType;
331336
}
332337
}
333338

0 commit comments

Comments
 (0)