-
Notifications
You must be signed in to change notification settings - Fork 367
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
Remove charset from "Content-Type" header #184
Comments
Looks like charset ("encoding") is always added on Content-Type in case it's not set: For good measure I checked with the RFCs and it seems the "parameter" part of the Content-Type value is either mandatory or optional depending on the particular MIME type/subtype: As far as I can tell the 'application/json' MIME type does explicitly not define a "charset" parameter: Ultimately this points to the http package being buggy. I don't think it's valid to blindly add parameters to just any MIME type. In fact, while probably done with good intentions, silently adding anything should probably be avoided. |
Work-around: Set the body first (this sets default headers), then set the headers. |
I have the same problem, however the above solution did not work in my case. |
Remove charset from "Content-Type" header Now the null value will be reflected for the charset in the header.
Remove charset from "Content-Type" header Now the null value will be reflected for the charset in the header.
Remove charset from "Content-Type" header Now the null value will be reflected for the charset in the header.
Remove charset from "Content-Type" header Now the null value will be reflected for the charset in the header.
For clarity, this is what worked for me: |
Remove charset from "Content-Type" header Now the null value will be reflected for the charset in the header.
I encountered the same issue, but @jheyne workaround didn't work for me. We should be able to write |
Good afternoon, I have the same problem. The lib is automatically placing charset = utf-8 in the request header, so I'm having trouble consuming some REST WebServices, making it impossible to use it. The issue is already open for one year. Is there no solution to such a problem? Thank you. Cristian Regazzo |
Has anyone solved this yet? It seems to be more of a problem when there encoding more complex types such as
I've tried jsonEncode, I've used the JSONSerializable package, setting the body before headers, just as @jheyne mentioned. I don't understand why the HttpClient encodes to UTF-8 by default. Either way, life is very difficult right now. I've even added UTF-8 encoding to my .NET server and still nothing :( Marc |
Has anybody found a solution to this yet? For now I have a local solution by copying the Package locally and modifying lib/src/request.dart to remove the lines that add charset if it's not there: (based on @cskau-g 's comment earlier) request.dart
You can find that code excerpt at https://github.com/dart-lang/http/blob/master/lib/src/request.dart#L84-L92 @jefioliveira tried to made a pull request for a similar change but it was understandably denied because a lot of applications probably rely on the current auto-addition of charset. It's been six months since then. Would it be possible to just add an additional optional parameter to the Request class that would tell it not to add charset? |
@jheyne solution worked for me. |
I had problem with the API server not accepting "charset". @jheyne's solution worked perfectly. |
@jheyne's answer worked for me. This is my code. import 'package:http/http.dart';
final String url = "your api url";
try {
final httpClient = HttpClient();
final request = await httpClient.postUrl(Uri.parse(url));
// headers
request.headers.set('Authorization', 'Bearer $AuthToken');
request.headers.contentType = new ContentType("application", "json");
// body
request.add(
utf8.encode(
jsonEncode(
{
'somebody': somebody,
},
),
),
);
final response = await request.close();
if (response.statusCode == 200) {
final responseBody = await response.transform(utf8.decoder).join();
final result = Model.fromJson(json.decode(responseBody));
}
} on TimeoutException catch (_) {
//print
} on SocketException catch (_) {
//print
}
|
This is a complete non sense : why is the charset added even if we provide the content-type ? The user input should override the default values, not the opposite ! |
Are there any updates regarding this topic?
Also I use Json Api Specification for Flutter. I cannot set the body first like @jheyne mentioned because the library makes the request.
|
@snowLimit I faced the same issues and decided to use this library https://pub.dev/packages/dio. It's very similar but more flexible and has a lot of interesting features 😉 |
it works for me |
this is ridiculous migrated to Dio |
Unbelievable that you'd have to pick a library over the langs native HTTP client. I've never seen a language where the HTTP client is so bad. |
Preventing charset from being added to the "content-type" header. Based on a workaround for a know http bug: dart-lang/http#184 (comment).
Has anything changed yet? |
@brianquinlan I think that this issue should be closed , current http package supports headers and with charsets. I dont think charsets removal is beneficial. Otherwise we can help adding more fields such as json , jsonwithnonCharset. But it doesnt make sense. |
It's possible to use Fetch API directly, if you are build for Web Platform only: import 'dart:js' as js;
var options = js.JsObject(js.context['Object']);
var headers = js.JsObject(js.context['Object']);
headers['Content-Type'] = 'application/json';
options['method'] = 'POST';
options['headers'] = headers;
options['body'] = jsonEncode(<String, String>{
'name': 'Flutter',
});
var result = await js.context.callMethod(
'fetch',
[
'https://some.place.com/foo/bar',
options
],
); But colleague suggest better way — https://pub.dev/packages/dio Result looks like: final dio = Dio();
final response = await dio.post(
'https://some.place.com/foo/bar',
data: jsonEncode(<String, String>{
'name': 'Flutter',
}),
options: Options(
headers: {},
contentType: 'application/json',
),
);
log(jsonEncode(response.data)); |
This seems to be working as designed and documented. If you want to assign set the body without setting the final request = Request('GET', myUrl)
..bodyBytes = utf8.encode(myJson)
..headers['content-type'] = 'application/json'; ? If that is the case, please let me know and I will update the documentation to demonstrate this pattern. |
Let's try to follow the code of conduct and be kind to each other. |
I did the same thing my friend, Dio is wasome. As soon as I find time I intend to submit a correction to this lib again, add an optional parameter or something like that. I understand that the solution was denied because I inadequately described the problem, but I intend to redo it soon. |
Is there any more feedback on this? Does the pattern suggested in #184 (comment) fix the issue? |
Hello,
I need to post some data to a distant API. But it looks like the API wants
Content-Type: application/json
but I'm sending aContent-Type: application/json; charset=utf-8
.I forced the headers:
but keep sending with the charset. Is there a possibility to remove it from the
Content Type
header ?Thanks
The text was updated successfully, but these errors were encountered: