lejard-h / chopper

Chopper is an http client generator using source_gen and inspired from Retrofit.
https://hadrien-lejard.gitbook.io/chopper
Other
713 stars 123 forks source link

Cannot change content-type #89

Closed sarinupreti closed 5 years ago

sarinupreti commented 5 years ago

generated code by chopper :

  Future<Response<BaseResponse<RefreshTokenResponse>>> updateRefreshToken(
      RefreshTokenRequest body) {
    final $url = '/user/refresh';
    final $headers = {'content-type': 'application/json'};
    final $body = body;
    final $request =
        Request('POST', $url, client.baseUrl, body: $body, headers: $headers);
    return client.send<BaseResponse<RefreshTokenResponse>,
        RefreshTokenResponse>($request);
  }

console:

flutter: INFO :  2019-09-02 13:48:32.242076 : --> POST http://{url}/user/refresh
flutter: INFO :  2019-09-02 13:48:32.242393 : Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJBdXRoZW50aWNhdGlvbiIsInBob25lTnVtYmV**************************************************************************
flutter: INFO :  2019-09-02 13:48:32.242854 : content-type: text/plain; charset=utf-8
flutter: INFO :  2019-09-02 13:48:32.245217 : {"refresh":"Refresh eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJSZWZyZXNoIiwicGhvbmVOdW1iZXIiOiIrOTc3************************************************************************"}
flutter: INFO :  2019-09-02 13:48:32.252074 : --> END POST (281-byte body)

the content-type does not seem to change.

pishguy commented 5 years ago

@sarinupreti you can override Header by passing string to the function which you want to connect to server,

that means

{@Header() String name}

for example:

  @Post()
  Future<Response<Resource>> newResource(@Body() Resource resource,
      {@Header() String name});
lejard-h commented 5 years ago

Do you have any other interceptor that could override it ?

crozarakamilla commented 2 years ago

I am still getting the same error even after setting the headers on patch method:

@Patch(path: "{id}", headers: {"Content-Type": "application/vnd.api+json"})  
Future<Response<CustomObj>> patch(@Path('id') String id, @Body() CustomObj data, @Header("Content-Type") String header);

And using an Interceptor:

class HeaderInterceptor implements RequestInterceptor {
  static const String AUTH_HEADER = "Authorization";
  static const String BEARER = "Bearer ";
  static const String V4_AUTH_HEADER = '';

  @override
  FutureOr<Request> onRequest(Request request) async {
    var token = await createAuthHeader();
    Request newRequest = request.copyWith(headers: {
      "Content-Type": "application/vnd.api+json",
      "Accept": "application/vnd.api+json",
      AUTH_HEADER: BEARER + token
    });
    return newRequest;
  }

class ChopperClientBuilder {
  static ChopperClient buildChopperClient(List<ChopperService> services,
          [http.BaseClient? httpClient]) =>
      ChopperClient(
        baseUrl: ApiPath.kApiURL.toString(),
        interceptors: [
          HeaderInterceptor(),
          HttpLoggingInterceptor(),
          CurlInterceptor(),
        ],
        services: services,
        converter: JsonSerializableConverter(),
      );
}

I've noticed that only the PATCH request still with the charset=utf-8:

 GET http://10.0.2.2:5000/api/v2.0/api-endpoint
I/flutter (10470): INFO: 2022-02-10 20:13:28.323118: Content-Type: application/vnd.api+json
I/flutter (10470): INFO: 2022-02-10 20:13:28.323237: Accept: application/vnd.api+json
I/flutter (10470): INFO: 2022-02-10 20:13:28.323299: Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJKUUZKUk
I/flutter (10470): INFO: 2022-02-10 20:13:28.323474: --> END GET
PATCH http://10.0.2.2:5000/api/v2.0/api-endpoint
I/flutter (10470): INFO: 2022-02-10 20:16:01.494242: Content-Type: application/vnd.api+json; charset=utf-8
I/flutter (10470): INFO: 2022-02-10 20:16:01.494520: Accept: application/vnd.api+json
I/flutter (10470): INFO: 2022-02-10 20:16:01.494791: Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJKUUZKUkF
I/flutter (10470): INFO: 2022-02-10 20:16:01.496249: {"type":"boletim-trafego","id":"112924","data_partida":"2022-02-10T19:00:00.000000Z","horario_agendado":"2022-02-04T19:00:00.000000Z","hodometro_inicial":16873.0,"hodometro_final":null,"itinerario":"Teste","veiculo":{"type":"veiculo","id":"34","placa":"JFQ8015"},"tipo_boletim":{"type":"boletim-trafego-tipo","id":"5","tipo_boletim":"Lavagem"},"local_partida":null,"observacoes":"","usuarios":[]}
I/flutter (10470): INFO: 2022-02-10 20:16:01.496743: --> END PATCH 
I/flutter (10470): INFO: 2022-02-10 20:16:01.511156: <-- 415 http://10.0.2.2:5000/api/v2.0/api-endpoint
I/flutter (10470): INFO: 2022-02-10 20:16:01.511390: content-type: application/vnd.api+json
I/flutter (10470): INFO: 2022-02-10 20:16:01.511548: date: Thu, 10 Feb 2022 23:16:01 GMT
I/flutter (10470): INFO: 2022-02-10 20:16:01.511692: access-control-allow-origin: *
I/flutter (10470): INFO: 2022-02-10 20:16:01.511832: server: Werkzeug/1.0.1 Python/3.9.2
I/flutter (10470): INFO: 2022-02-10 20:16:01.511982: content-length: 177
I/flutter (10470): INFO: 2022-02-10 20:16:01.512203: {"errors": [{"source": "", "detail": "Content-Type header must be application/vnd.api+json", "title": "Invalid request header", "status": "415"}], "jsonapi": {"version": "1.0"}}

If I send the PATCH request using curl and remove the charset=utf-8 it works just fine.

What am I missing here? Is there another way to override the headers using chopper?

I had to tweak my backend and use a middleware to intercept these requests and override the headers, but ideally it should be done on the frontend.