No content even though contentLength was specified to be greater than 0: 134
New Issue Checklist
- [x] I have searched for a similar issue in the project and found none
Issue Info
| Info | Value | |
|---|---|---|
| Platform Name | flutter / Android | |
| Platform Version | >=2.1.0 <3.0.0 | |
| Dio Version | ^2.1.7 | |
| Android Studio | Android Studio 3.3.2 |
Issue Description and Steps
I got while trying to make GET request to API
Exception occurred: DioError [DioErrorType.DEFAULT]: HttpException: No content even though contentLength was specified to be greater than 0: 134.,
uri = https://app.meatified.com/api/v1/transactions/verify/1e7807a0-80b4-4951-b6f0-72b329010bda stackTrace: #0
The GET request is not hinting my endpoint, See log below for more info
Below is my Interceptor class
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:meatify/app/resources/firebase_auth_provider.dart';
class MeatityHttpInterceptor implements Interceptor {
Map<String, dynamic> _header;
FireBaseAuthProvider _auth;
MeatityHttpInterceptor() {
_auth = new FireBaseAuthProvider();
_header = {
HttpHeaders.contentTypeHeader: "application/json"
};
}
@override
onError(DioError err) {
// TODO: implement onError
return null;
}
@override
onRequest(RequestOptions options) async {
//Hope this will work.
FirebaseUser loggedInUser = await _auth.currentUser();
if(loggedInUser != null){
String token = await loggedInUser.getIdToken(refresh: true);
_header.addAll({HttpHeaders.authorizationHeader: token});
}
options.connectTimeout = 10000;
options.responseType = ResponseType.json;
options.baseUrl = "https://app.meatified.com/api/v1";
options.contentType = ContentType.parse("application/json");
options.receiveTimeout = 10000;
options.headers = _header;
print('send request:path:${options.path},baseURL:${options.baseUrl}');
return options;
}
@override
onResponse(Response response) {
print('HTTP INTERCEPTOR RESPONSE');
print(response.data);
// print(response.realUri);
return null;
}
}
Will appreciate your quick response on this issue.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.
please update to latest version and retry.
already latest version. i have same problem.
already latest version. i have same problem. Have you solved the problem
Have you solved the problem
Facing same issue, when calling multiple APIs at the same time, like POST and GET at the same time when POST with body content and GET without body content, And somehow the GET request header get override by POST, In result dio throws exception saying GET request does not have body but the content-length is specified,
To solve: I have added interceptor to remove content-length header while requesting but no luck!
Anyone else facing this kind of issue and above scenario? Any solution?
Facing same issue, when calling multiple APIs at the same time, like POST and GET at the same time when POST with body content and GET without body content, And somehow the GET request header get override by POST, In result dio throws exception saying GET request does not have body but the content-length is specified,
To solve: I have added interceptor to remove content-length header while requesting but no luck!
Anyone else facing this kind of issue and above scenario? Any solution?
Don't redefine Headers : options.headers = headers; should options.headers['xxx']=xxx; hope this helps
My case produces the same error:
_data= FormData.fromMap(Map<String, String>.from(jsonDecode(myParamsBody)));
var dio = Dio();
var baseOptions = RequestOptions(path: url, method: "post", headers: _headers, data: _data);
baseOptions.headers["content-length"] = (baseOptions.data as FormData).length;
response = await dio.fetch(baseOptions);
Exception:
DioError [DioErrorType.other]: HttpException: No content even though contentLength was specified to be greater than 0: 222., uri...
#0 DioMixin.fetch.<anonymous closure> (package:dio/src/dio_mixin.dart:643:7)
#1 _rootRunBinary (dart:async/zone.dart:1450:47)
#2 _CustomZone.runBinary (dart:async/zone.dart:1342:19)
#3 _FutureListener.handleError (dart:async/future_impl.dart:162:22)
...
I solved it by doing: (Uppercase to method)
var baseOptions = RequestOptions(path: url, method: "POST", headers: _headers, data: _data);
The reason for the solution: The library validates the method with UPPERCASE. -> dio_mixin.dart -> allowPayloadMethods
Future<Stream<Uint8List>?> _transformData(RequestOptions options) async {
var data = options.data;
List<int> bytes;
Stream<List<int>> stream;
const allowPayloadMethods = ['POST', 'PUT', 'PATCH', 'DELETE'];
if (data != null && allowPayloadMethods.contains(options.method)) {
// Handle the FormData
int? length;
if (data is Stream) {
assert(data is Stream<List>,
'Stream type must be `Stream<List>`, but ${data.runtimeType} is found.');
stream = data as Stream<List<int>>;
options.headers.keys.any((String key) {
if (key.toLowerCase() == Headers.contentLengthHeader) {
length = int.parse(options.headers[key].toString());
return true;
}
return false;
});
} else if (data is FormData) {
options.headers[Headers.contentTypeHeader] =
'multipart/form-data; boundary=${data.boundary}';
stream = data.finalize();
length = data.length;
options.headers[Headers.contentLengthHeader] = length.toString();
} else {
// Call request transformer.
var _data = await transformer.transformRequest(options);
if (options.requestEncoder != null) {
bytes = options.requestEncoder!(_data, options);
} else {
//Default convert to utf8
bytes = utf8.encode(_data);
}
// support data sending progress
length = bytes.length;
options.headers[Headers.contentLengthHeader] = length.toString();
var group = <List<int>>[];
const size = 1024;
var groupCount = (bytes.length / size).ceil();
for (var i = 0; i < groupCount; ++i) {
var start = i * size;
group.add(bytes.sublist(start, math.min(start + size, bytes.length)));
}
stream = Stream.fromIterable(group);
}
return addProgress(stream, length, options);
}
return null;
}