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

Commit

Permalink
Fix ContentType parsing in HttpClient utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jan 31, 2022
1 parent 4a4eea8 commit b05dfff
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/ServiceStack.Text/HttpUtils.HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public static string SendStringToUrl(this HttpClient client, string url, string
{
httpReq.Content = new StringContent(requestBody, UseEncoding);
if (contentType != null)
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
}
requestFilter?.Invoke(httpReq);

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

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

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

Expand Down Expand Up @@ -725,7 +725,7 @@ public static Stream SendStreamToUrl(this HttpClient client, string url, string
{
httpReq.Content = new StreamContent(requestBody);
if (contentType != null)
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(contentType);
}
requestFilter?.Invoke(httpReq);

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

Expand Down Expand Up @@ -1058,10 +1058,7 @@ public static HttpRequestMessage WithHeader(this HttpRequestMessage httpReq, str
{
if (httpReq.Content == null)
throw new NotSupportedException("Can't set ContentType before Content is populated");
httpReq.Content.Headers.ContentType = new MediaTypeHeaderValue(value.LeftPart(';'));
var charset = value.RightPart(';');
if (charset != null && charset.IndexOf("charset", StringComparison.OrdinalIgnoreCase) >= 0)
httpReq.Content.Headers.ContentType.CharSet = charset.RightPart('=');
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(value);
}
else if (name.Equals(HttpHeaders.Referer, StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -1097,7 +1094,11 @@ public static HttpRequestMessage With(this HttpRequestMessage httpReq, Action<Ht
if (config.UserAgent != null)
headers.Add(new(HttpHeaders.UserAgent, config.UserAgent));
if (config.ContentType != null)
headers.Add(new(HttpHeaders.ContentType, config.ContentType));
{
if (httpReq.Content == null)
throw new NotSupportedException("Can't set ContentType before Content is populated");
httpReq.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(config.ContentType);
}
if (config.Referer != null)
httpReq.Headers.Referrer = new Uri(config.Referer);
if (config.Authorization != null)
Expand Down

0 comments on commit b05dfff

Please sign in to comment.