-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StringContent doesn't have an option to remove charset #1478
Comments
These are the current public constructors of public StringContent(string content);
public StringContent(string content, Encoding encoding);
public StringContent(string content, Encoding encoding, string mediaType); All of them have the behavior of adding a But we could possibly add a new constructor overload that takes a Proposal: public StringContent(string content, MediaTypeHeaderValue mediaTypeHeaderValue); |
I've just found another way to remove var content = new StringContent(json, Encoding.UTF8, "application/json");
content.Headers.ContentType.CharSet = string.Empty; |
The second way looks good! If we expose an API, i don't think the mediatypeheadervalue allows Encoding to be set, it there also needs to be public StringContent(string content, MediaTypeHeaderValue mediaType);
public StringContent(string content, Encoding encoding, MediaTypeHeaderValue mediaType); It's not clear and intuitive to figure out how to get the charset to be unset, exposing APIs sounds good. @davidsh What do you think? |
Triage: We need a little bit of validation if it works and is sufficient. Then we can send it to API review. |
Thank you very much. |
Added 2 ctors to StringContent. Both allow initialization with a custom MediaTypeHeaderValue object. This change was proposed in issue dotnet#1478 to be able to omit the charset label from the Content-type HTTP header when it is rendered. Fix dotnet#1478
@ckadluba, It would help us push this through API review if you can fulfill @karelz's ask:
If you can post some examples of how using this API will compare with the workaround that @hungquach identified, that will help demonstrate any benefits. Next review is 1/28, so we have some time. |
Thanks for clarifying @scalablecory. I'm not sure if the following is what you need for the API review, but i'll give it a try. If there is anything unclear or something else is required, please let me know. Here is how to create a var content = new StringContent(json,
new MediaTypeHeaderValue("application/json")
{
CharSet = string.Empty
}); Compared to creating the same with the existing workaround. var content = new StringContent(json, "application/json");
content.Headers.ContentType.CharSet = string.Empty; The first variant has the benefit that With the second added ctor, also custom encodings (other than default UTF-8) could be used. var content = new StringContent(json, Encoding.UTF7
new MediaTypeHeaderValue("application/json")); But this can also be done with the existing ctor with the signature var content = new StringContent(json, Encoding.UTF7, "application/json"); The charset is in both cases deducted from the passed Actually these scenarios are not a yet a real benefit over the workaround. Making the full object graph immutable would probably mean significant changes in all involved classes. But the added constructors would at least remove one obstacle when going into that direction, while still allowing to customize charset and other properties. |
The related item https://github.com/dotnet/corefx/issues/7864 was discussed and approved in today's API review. Closing this item. |
API shape:
Original proposal
I'm using
HttpClient
to send the request but it seemsStringContent
doesn't have an option to removecharset
and I have to do it manuallyREST endpoint only support
{application/json}
not{application/json; charset=utf-8}
Environment: .NET Core 2.0, .NET Standard 2.0
[EDIT] Add C# syntax highlighting by @karelz
The text was updated successfully, but these errors were encountered: