Skip to content
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

Streamline requests' _jsonRequest and _multipartRequest body #48

Closed
Nerglej opened this issue Sep 8, 2023 · 1 comment
Closed

Streamline requests' _jsonRequest and _multipartRequest body #48

Nerglej opened this issue Sep 8, 2023 · 1 comment

Comments

@Nerglej
Copy link

Nerglej commented Sep 8, 2023

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.

@ganigeorgiev
Copy link
Member

Should be fixed in v0.15.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants