You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When working on a little project I have, I stumbled upon some seemingly random "null" text strings in PocketBase in text fields. I found out that the difference is when a file was uploaded or not. If I send a file with the request, the _multipartRequest uses a custom little function to add all values in the body:
http.MultipartRequest _multipartRequest(
String method,
Uri url, {
Map<String, String> headers = const {},
Map<String, dynamic> body = const {},
List<http.MultipartFile> files = const [],
}) {
final request = http.MultipartRequest(method, url)
..files.addAll(files)
..headers.addAll(headers);
body.forEach((key, value) {
if (value is Iterable) {
for (var i = 0; i < value.length; i++) {
request.fields["$key[$i]"] = value.elementAt(i).toString();
}
} else {
request.fields[key] = value.toString();
}
});
return request;
}
Comparing this to when there's no file present in the request:
http.Request _jsonRequest(
String method,
Uri url, {
Map<String, String> headers = const {},
Map<String, dynamic> body = const {},
}) {
final request = http.Request(method, url);
if (body.isNotEmpty) {
request.body = jsonEncode(body);
}
if (headers.isNotEmpty) {
request.headers.addAll(headers);
}
if (!headers.containsKey("Content-Type")) {
request.headers["Content-Type"] = "application/json";
}
return request;
}
The key point to not is in the _jsonRequest function we just use jsonEncode to encode the body. This makes null values actually be null, instead of the _multipartRequest where it's stringyfied. My guess is that there's other values like this, like numbers and booleans that's affected too, but I'm not sure if PocketBase cares about those. I just noticed it with specifically null-values, in file-uploads. My simple fix for the time being is the following code:
http.MultipartRequest _multipartRequest(
String method,
Uri url, {
Map<String, String> headers = const {},
Map<String, dynamic> body = const {},
List<http.MultipartFile> files = const [],
}) {
final request = http.MultipartRequest(method, url)
..files.addAll(files)
..headers.addAll(headers);
body.forEach((key, value) {
if (value is Iterable) {
for (var i = 0; i < value.length; i++) {
request.fields["$key[$i]"] = value.elementAt(i).toString();
}
} else {
if (value == null) request.fields[key] = null;
else request.fields[key] = value.toString();
}
});
return request;
}
I just added a simple if-statement to specifically say the key is null, instead of using toString(). I don't really think the value is even necessary, but the jsonEncode function still has all null-values, so I also let it stay to streamline.
The text was updated successfully, but these errors were encountered:
When working on a little project I have, I stumbled upon some seemingly random "null" text strings in PocketBase in text fields. I found out that the difference is when a file was uploaded or not. If I send a file with the request, the _multipartRequest uses a custom little function to add all values in the body:
Comparing this to when there's no file present in the request:
The key point to not is in the _jsonRequest function we just use jsonEncode to encode the body. This makes null values actually be null, instead of the _multipartRequest where it's stringyfied. My guess is that there's other values like this, like numbers and booleans that's affected too, but I'm not sure if PocketBase cares about those. I just noticed it with specifically null-values, in file-uploads. My simple fix for the time being is the following code:
I just added a simple if-statement to specifically say the key is null, instead of using toString(). I don't really think the value is even necessary, but the jsonEncode function still has all null-values, so I also let it stay to streamline.
The text was updated successfully, but these errors were encountered: