Skip to content

Properly handle null field values when encoding requests. #77

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import 'byte_stream.dart';
/// mapToQuery({"foo": "bar", "baz": "bang"});
/// //=> "foo=bar&baz=bang"
String mapToQuery(Map<String, String> map, {Encoding encoding}) {
var nullKeys = map.keys.where((k) => (map[k] == null)).toList();
nullKeys.forEach(map.remove); // -- remove elements with null values

var pairs = <List<String>>[];
map.forEach((key, value) =>
pairs.add([Uri.encodeQueryComponent(key, encoding: encoding),
Expand Down
9 changes: 6 additions & 3 deletions test/io/http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ main() {
'User-Agent': 'Dart'
}, body: {
'some-field': 'value',
'other-field': 'other value'
'other-field': 'other value',
'null-field': null
}).then((response) {
expect(response.statusCode, equals(200));
expect(response.body, parse(equals({
Expand Down Expand Up @@ -229,7 +230,8 @@ main() {
'User-Agent': 'Dart'
}, body: {
'some-field': 'value',
'other-field': 'other value'
'other-field': 'other value',
'null-field': null
}).then((response) {
expect(response.statusCode, equals(200));
expect(response.body, parse(equals({
Expand Down Expand Up @@ -333,7 +335,8 @@ main() {
'User-Agent': 'Dart'
}, body: {
'some-field': 'value',
'other-field': 'other value'
'other-field': 'other value',
'null-field': null
}).then((response) {
expect(response.statusCode, equals(200));
expect(response.body, parse(equals({
Expand Down