Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b05dfff

Browse files
committed
Fix ContentType parsing in HttpClient utils
1 parent 4a4eea8 commit b05dfff

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Diff for: src/ServiceStack.Text/HttpUtils.HttpClient.cs

+12-11
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public static string SendStringToUrl(this HttpClient client, string url, string
500500
{
501501
httpReq.Content = new StringContent(requestBody, UseEncoding);
502502
if (contentType != null)
503-
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
503+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
504504
}
505505
requestFilter?.Invoke(httpReq);
506506

@@ -531,7 +531,7 @@ public static async Task<string> SendStringToUrlAsync(this HttpClient client, st
531531
{
532532
httpReq.Content = new StringContent(requestBody, UseEncoding);
533533
if (contentType != null)
534-
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
534+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
535535
}
536536
requestFilter?.Invoke(httpReq);
537537

@@ -612,7 +612,7 @@ public static byte[] SendBytesToUrl(this HttpClient client, string url, string m
612612
{
613613
httpReq.Content = new ReadOnlyMemoryContent(requestBody);
614614
if (contentType != null)
615-
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
615+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
616616
}
617617
requestFilter?.Invoke(httpReq);
618618

@@ -643,7 +643,7 @@ public static async Task<byte[]> SendBytesToUrlAsync(this HttpClient client, str
643643
{
644644
httpReq.Content = new ReadOnlyMemoryContent(requestBody);
645645
if (contentType != null)
646-
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
646+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
647647
}
648648
requestFilter?.Invoke(httpReq);
649649

@@ -725,7 +725,7 @@ public static Stream SendStreamToUrl(this HttpClient client, string url, string
725725
{
726726
httpReq.Content = new StreamContent(requestBody);
727727
if (contentType != null)
728-
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
728+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
729729
}
730730
requestFilter?.Invoke(httpReq);
731731

@@ -756,7 +756,7 @@ public static async Task<Stream> SendStreamToUrlAsync(this HttpClient client, st
756756
{
757757
httpReq.Content = new StreamContent(requestBody);
758758
if (contentType != null)
759-
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
759+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
760760
}
761761
requestFilter?.Invoke(httpReq);
762762

@@ -1058,10 +1058,7 @@ public static HttpRequestMessage WithHeader(this HttpRequestMessage httpReq, str
10581058
{
10591059
if (httpReq.Content == null)
10601060
throw new NotSupportedException("Can't set ContentType before Content is populated");
1061-
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(value.LeftPart(';'));
1062-
var charset = value.RightPart(';');
1063-
if (charset != null && charset.IndexOf("charset", StringComparison.OrdinalIgnoreCase) >= 0)
1064-
httpReq.Content.Headers.ContentType.CharSet = charset.RightPart('=');
1061+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(value);
10651062
}
10661063
else if (name.Equals(HttpHeaders.Referer, StringComparison.OrdinalIgnoreCase))
10671064
{
@@ -1097,7 +1094,11 @@ public static HttpRequestMessage With(this HttpRequestMessage httpReq, Action<Ht
10971094
if (config.UserAgent != null)
10981095
headers.Add(new(HttpHeaders.UserAgent, config.UserAgent));
10991096
if (config.ContentType != null)
1100-
headers.Add(new(HttpHeaders.ContentType, config.ContentType));
1097+
{
1098+
if (httpReq.Content == null)
1099+
throw new NotSupportedException("Can't set ContentType before Content is populated");
1100+
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(config.ContentType);
1101+
}
11011102
if (config.Referer != null)
11021103
httpReq.Headers.Referrer = new Uri(config.Referer);
11031104
if (config.Authorization != null)

0 commit comments

Comments
 (0)