Skip to content

Unable to send request with bodyBytes set when using streamRequests #23

@TheOneWithTheBraid

Description

@TheOneWithTheBraid

When trying to send a request with Request.bodyBytes set, I realized the request is given a conversion error as body rather than the actually provided body.

Minimal sample to reproduce :

import 'dart:convert';

import 'package:fetch_client/fetch_client.dart';
import 'package:http/http.dart';

Future<void> main() async {
  final client = FetchClient(
    mode: RequestMode.cors,
    credentials: RequestCredentials.omit,
    cache: RequestCache.noStore,
    referrerPolicy: RequestReferrerPolicy.strictOriginWhenCrossOrigin,
    streamRequests: true,
  );

  final request = Request(
    'POST',
    Uri.parse('https://example.com'),
  );

  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(jsonEncode({'test': 'foo'}));
  final response = await client.send(request);
  final responseBody = await response.stream.toBytes();

  final responseString = utf8.decode(responseBody);
  print(responseString);
}

When inspecting the request in the dev tools of my browser, the request body shows as :

[object ReadableStream]

Rather than the expected

{"test":"foo"}

I'd expect a fallback on non-streamed requests if the browser does not support them.

Environment:

Firefox 139.0.1 on Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions