Connection closed while receiving data Dart + Flask

110 Views Asked by At

Hello there.

I have this error when I trying to make post request to the server.

Unhandled exception:
Connection closed while receiving data
#0      IOClient.send.<anonymous closure> (package:http/src/io_client.dart:76:13)
#1      _invokeErrorHandler (dart:async/async_error.dart:45:24)
#2      _HandleErrorStream._handleError (dart:async/stream_pipe.dart:269:9)
#3      _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:157:13)
#4      _HttpClientResponse.listen.<anonymous closure> (dart:_http/http_impl.dart:709:16)
#5      _RootZone.runBinaryGuarded (dart:async/zone.dart:1630:10)
#6      _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#7      _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:7)
#8      _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:280:7)
#9      _ForwardingStreamSubscription._addError (dart:async/stream_pipe.dart:128:11)
#10     _addErrorWithReplacement (dart:async/stream_pipe.dart:176:8)
#11     _HandleErrorStream._handleError (dart:async/stream_pipe.dart:274:11)
#12     _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:157:13)
#13     _RootZone.runBinaryGuarded (dart:async/zone.dart:1630:10)
#14     _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#15     _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:378:7)
#16     _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:280:7)
#17     _SyncStreamControllerDispatch._sendError (dart:async/stream_controller.dart:778:19)
#18     _StreamController._addError (dart:async/stream_controller.dart:656:7)
#19     _StreamController.addError (dart:async/stream_controller.dart:610:5)
#20     _HttpParser._reportBodyError (dart:_http/http_parser.dart:1176:22)
#21     _HttpParser._onDone (dart:_http/http_parser.dart:885:9)
#22     _RootZone.runGuarded (dart:async/zone.dart:1606:10)
#23     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:394:13)
#24     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:404:7)
#25     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
#26     _SyncStreamControllerDispatch._sendDone (dart:async/stream_controller.dart:782:19)
#27     _StreamController._closeUnchecked (dart:async/stream_controller.dart:637:7)
#28     _StreamController.close (dart:async/stream_controller.dart:630:5)
#29     _Socket._onError (dart:io-patch/socket_patch.dart:2338:19)
#30     _RootZone.runBinaryGuarded (dart:async/zone.dart:1630:10)
#31     _BufferingStreamSubscription._sendError.sendError (dart:async/stream_impl.dart:360:15)
#32     _BufferingStreamSubscription._sendError (dart:async/stream_impl.dart:375:9)
#33     _BufferingStreamSubscription._addError (dart:async/stream_impl.dart:280:7)
#34     _SyncStreamControllerDispatch._sendError (dart:async/stream_controller.dart:778:19)
#35     _StreamController._addError (dart:async/stream_controller.dart:656:7)
#36     _StreamController.addError (dart:async/stream_controller.dart:610:5)
#37     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1852:23)
#38     _RootZone.runBinaryGuarded (dart:async/zone.dart:1630:10)
#39     _RootZone.bindBinaryCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1662:33)
#40     _NativeSocket.reportError (dart:io-patch/socket_patch.dart:1562:32)
#41     _NativeSocket.multiplex (dart:io-patch/socket_patch.dart:1408:13)
#42     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

This is dart code (not working)

import 'dart:convert';

import 'package:http/http.dart';

const String kDefaultServerApiUrl = "http://192.168.0.12:8080/api/login";

void main() async {
  var client = Client();
  try {
    var response = await client.post(Uri.parse(kDefaultServerApiUrl),
        body: {"username": "06ED", "password": "1234"});
    print(utf8.decode(response.bodyBytes));
    print(response.body);
    var decodedResponse = jsonDecode(utf8.decode(response.bodyBytes)) as Map;

    print(decodedResponse);
  } finally {
    client.close();
  }
}

This is part of flask server (I'm using flask_restful)

import base64

from flask_restful import Resource
from data import db_session
from flask import jsonify, request

from data.users import Users


class LoginResource(Resource):
    @staticmethod
    def post():
        nickname = request.json["nickname"]
        password = request.json["password"]

        session = db_session.create_session()
        user: Users = session.query(Users).filter(Users.nickname == nickname).first()
        if not user:
            return jsonify({"err": "Wrong nickname"})

        if user.check_password(password):
            return jsonify({
                "id": user.id,
                "nickname": user.nickname,
                "mail": user.email,
                "image": base64.b64encode(user.image).decode("utf-8")
            })

        return jsonify({"err": "Wrong password"})

main.py (part of flask server)

app = Flask(__name__)
app.config['SECRET_KEY'] = 'key'

login_manager = LoginManager()
login_manager.init_app(app)

api = Api(app)
api.add_resource(LoginResource, "/api/login/")

But I haven't this problem when I trying to do this request on python

import base64

import requests


user = requests.post(
    "http://192.168.0.12:8080/api/login",
    json={
        "nickname": "06ED",
        "password": "1234"
    }
).json()
print(user)
with open("img.png", "wb") as file:
    file.write(base64.b64decode(user["image"]))

P.S. I tried to make this request in flutter app, but it not working too

0

There are 0 best solutions below