Home > database >  Flutter Http status error [415] (using Retrofit)
Flutter Http status error [415] (using Retrofit)

Time:08-24

I'm trying to send http request using Retrofit code generation.

I'm getting status 415, even though I added Accept json and Content-Type json to the header.

DioError [DioErrorType.response]: Http status error [415]

My retrofit code:

@POST('/users')
  Future<dynamic> users(
    @Body() Map<String, String> request, {
    @Header('accept') String accept = 'application/json',
    @Header('content-type') String contentType = 'application/json',
  });

The generated code:

@override
  Future<dynamic> users(request,
      {accept = 'application/json', contentType = 'application/json'}) async {
    const _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _headers = <String, dynamic>{
      r'accept': accept,
      r'content-type': contentType
    };
    _headers.removeWhere((k, v) => v == null);
    final _data = request;
    final _result = await _dio.fetch(_setStreamType<dynamic>(Options(
            method: 'POST',
            headers: _headers,
            extra: _extra,
            contentType: contentType)
        .compose(_dio.options, '/users',
            queryParameters: queryParameters, data: _data)
        .copyWith(baseUrl: baseUrl ?? _dio.options.baseUrl)));
    final value = _result.data;
    return value;
  }

CodePudding user response:

Please try this

"Content-Type", "application/json; charset=utf8"

CodePudding user response:

Solved!

Since the request type is Map<String, String>, I had to change the content-type to application/x-www-form-urlencoded

  • Related