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

[BUG] Always loading for multipart request #165

Open
paulmwk opened this issue Jan 9, 2024 · 0 comments
Open

[BUG] Always loading for multipart request #165

paulmwk opened this issue Jan 9, 2024 · 0 comments
Assignees
Labels
new issue New issue which has not been checked yet

Comments

@paulmwk
Copy link

paulmwk commented Jan 9, 2024

Describe the bug
If the request is a multipart request, it will constantly load in Alice.
Because in dart http package, the content-type header will be set to a new value with a random boundary after request interceptors.

The original request header is different from response.base.request!.headers.
Alice cannot match request and response, and it will constantly load.

https://github.com/dart-lang/http/blob/master/pkgs/http/lib/src/multipart_request.dart#L87

Below is response.base.request!.headers

{alice_token: 1704769019807, content-type: multipart/form-data; boundary=dart-http-boundary-AsZ9bc8Ke2mL_zsSENWVPk4wA3TvawN2LwJdX57EOMEh7YyHY1R}

To Reproduce

  1. create a Flutter application called demo_app
  2. install chopper, chopper_generator, http, http_parser and build_runner
  3. click the call API button
  4. request will constantly load

example_service.dart

@ChopperApi(baseUrl: '/')
abstract class ExampleService extends ChopperService {
  // A helper method that helps instantiating the service. You can omit this method and use the generated class directly instead.
  static ExampleService create([ChopperClient? client]) =>
      _$ExampleService(client);

  @Post(path: 'upload')
  @Multipart()
  Future<Response> postExample(
      {@PartFile('file') required MultipartFile files});
}

main.dart

import 'dart:async';

import 'package:alice/alice.dart';
import 'package:chopper/chopper.dart';
import 'package:demo_app/example_service.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

final GlobalKey<NavigatorState> navigatorKey = GlobalKey();

final alice = Alice(navigatorKey: navigatorKey, showNotification: false);

final chopper = ChopperClient(
  baseUrl: Uri.parse('https://example.com'),
  services: [
    ExampleService.create(),
  ],
  interceptors: [
    alice.getChopperInterceptor(),
  ],
);

void main() async {

  WidgetsFlutterBinding.ensureInitialized();
  // Run app.
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) => MaterialApp(
        navigatorKey: navigatorKey,
        home: const MyHomePage(),
      );
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  Future<void> callApi() async {
    final response = await chopper
        .getService<ExampleService>()
        .postExample(files: http.MultipartFile.fromString('img', '123'));
    print(response.base.request!.headers);
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('Demo'),
        ),
        body: SingleChildScrollView(
          child: Center(
            child: Column(
              children: [
                ElevatedButton(
                  onPressed: alice.showInspector,
                  child: const Text('Open Alice'),
                ),
                ElevatedButton(
                  onPressed: callApi,
                  child: const Text('Call API'),
                ),
              ],
            ),
          ),
        ),
      );
}

Expected behavior
Multipart request should not load constantly.
Response should be able to match the request using the getRequestHashCode method.

Additional context
NA

Alice Version
0.4.1

@paulmwk paulmwk added the new issue New issue which has not been checked yet label Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new issue New issue which has not been checked yet
Projects
None yet
Development

No branches or pull requests

2 participants