dio
dio copied to clipboard
Dio Throws Exception on DELETE Request with 204 Response
Package
dio
Version
5.5.0
Operating-System
Web
Adapter
Default Dio
Output of flutter doctor -v
[√] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.22631.3880], locale en-US)
• Flutter version 3.19.6 on channel stable at C:\src\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 54e66469a9 (4 months ago), 2024-04-17 13:08:03 -0700
• Engine revision c4cd48e186
• Dart version 3.3.4
• DevTools version 2.31.1
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
• Android SDK at C:\Users\andrew\AppData\Local\Android\sdk
• Platform android-34, build-tools 32.1.0-rc1
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[X] Visual Studio - develop Windows apps
X Visual Studio not installed; this is necessary to develop Windows apps.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2022.3)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
[√] VS Code (version 1.92.0)
• VS Code at C:\Users\andrew\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.94.0
[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22631.3880]
• Chrome (web) • chrome • web-javascript • Google Chrome 126.0.6478.127
• Edge (web) • edge • web-javascript • Microsoft Edge 127.0.2651.86
[√] Network resources
• All expected network resources are available.
Dart Version
3.3.4
Steps to Reproduce
Create a flutter application with dio as a dependency.
Send a DELETE request to an endpoint which returns a 204 "No Content" http status code.
Expected Result
The request goes through without throwing an exception.
Actual Result
The request throws a DioException with the message "SyntaxError: Unexpected end of JSON input".
Please let me know if there's any more info I can add. Judging by the error message it seems that Dio is attempting to parse the body of the response, but since the response body is null, it throws this error.
The issue does not occur in 5.4.3, so I have downgraded the version for now.
Only tested with DELETE requests, I am not sure if other HTTP requests are affected.
Tested only in flutter web, I am not sure if other platforms are affected.
Please add sample code that shows how you call the delete endpoint. Make sure to use the correct generic response type
Here's the basic implementation of my HttpProvider class. For brevity, some methods like sendGet and sendPost are removed. I also cut out irrelevant details (such as a try-catch around the _dio.delete() method to throw my own error), but I checked and the "FormatException: SyntaxError: Unexpected end of JSON input" error still occurs with the code below.
class HttpProvider {
HttpProvider({this.token}) {
_dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 6000)));
_dio.options.baseUrl = 'https://redacted'
}
String? token;
Future<Options> _getOptions() async {
Map<String, dynamic> headers = {
'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload',
};
if (token != null) {
headers['Authorization'] = "TOKEN ${token!}";
}
return Options(headers: headers);
}
Future<Response<dynamic>> sendDelete(String url, [data, String? token]) {
return _dio.delete(
url,
data: data,
options: await _getOptions(token),
);
}
Request body:
{"ids":[1536]}
Response headers:
HTTP/1.1 204 No Content
Server: nginx
Date: Mon, 12 Aug 2024 12:31:47 GMT
Content-Type: application/json
Content-Length: 352
Connection: keep-alive
Vary: Accept, Authorization, Cookie, Origin
Allow: PATCH, DELETE, OPTIONS
Cache-Control: no-cache, no-store, must-revalidate
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Cross-Origin-Opener-Policy: same-origin
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: DELETE
Access-Control-Allow-Headers: X-Device-UID,Strict-Transport-Security,Authorization,Content-Type
Access-Control-Max-Age: 1728000
It seems that this might be an issue with the latest transformer from #2239
You set ResponseType.json, and
The server sets
Content-Type: application/json
Content-Length: 352
so the transformer expects JSON content.
This is a violation of the HTTP Spec https://www.rfc-editor.org/rfc/rfc7230
A server MUST NOT send a Content-Length header field in any response with a status code of 1xx (Informational) or 204 (No Content).
But the transformer still probably should be adapted to handle this more gracefully.
For now, you can work around this by setting ResponseType to .bytes