Mindinventory / flutter-retrofit

API calls using the retrofit client.
https://www.mindinventory.com/flutter-app-development.php
MIT License
51 stars 21 forks source link

@Headers issue #6

Closed Gobmichet closed 2 years ago

Gobmichet commented 3 years ago

Hi, and thanks for your work,

I have some trouble with @headers that i just can't see in the DevTools while debugging (0_o)

what's more i HAVE to add this in imports : import 'package:dio/dio.dart' hide Headers; because if i don't, Android studio yells at me saying @Headers are both declared in dart and retrofit packages... Is it normal ??? Do i do the good thing ?

Just for tests i did implement in your github code the method below :

  @GET("/posts/{id}")
  @Headers(<String, dynamic>{
    "Content-Type" : "application/json",
    "Custom-Header" : "Your header"
  })
  Future<PostDataById> getPostById(@Path("id") int id);

And while the request does work (see screenshot), there is no way i can have a look at headers i did add :( As you can see on the screenshot :: Request headers content-length [0]

Can you please help/explain me ? Thanks in advance.

PS :: what's more in a "real" project of mine, when i add a certain header i got a 403 forbidden but a 401 unauthorized if i remove it... So obviously there are headers that are added but still can't see any of them in the flutter devtools :(

PPS :: is there any other way/mean to debug my request ? catching the request and printing headers somewhere in the code maybe ?????

Headers

happysingh23828 commented 2 years ago

did you find ant solutions for that? @Gobmichet

mi-ghanshyam commented 2 years ago

Initialize Dio with your custom header which is cost throughout the app.

dio = Dio(
      BaseOptions(contentType: 'application/json', headers: {
        "Custom-Header" : "Your header"
      }),
    );
nobimac commented 2 years ago

Hello @Gobmichet, There are 2 ways to manage header using retrofit

  1. You can use interceptors to setup you hedear E.g
    _dio!.interceptors.add(
    InterceptorsWrapper(
     onRequest: (options, handler) async {
       options.headers['token'] = 'token';
       return handler.next(options);
     },
    ),
    );
  2. Diractly add custom header on servcie call using @header param E.g
    @GET("/posts/{id}")
    Future<PostDataById> getPostById(@Path("id") int id, @Header('Content-Type') String header);

    I'll hope this will help you

Thank you!