Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use Dio post with python django API #236

Closed
wilk3ns opened this issue Mar 28, 2019 · 4 comments
Closed

Unable to use Dio post with python django API #236

wilk3ns opened this issue Mar 28, 2019 · 4 comments

Comments

@wilk3ns
Copy link

wilk3ns commented Mar 28, 2019

Hi there, i cant get post method working with dio. I have code like this. And when executed throws an error like in Logs.

Code

Future<Response> signUp(String phoneNumber, String uid, String fullName, File propic) async {
  var url = "${apiHost}api/sign_up/";

  print(
      "building request to $url and phone $phoneNumber and uid $uid and name $fullName");
  FormData formData = new FormData.from(<String, dynamic>{
    "full_name": fullName,
    "uid": uid,
    "phone": phoneNumber,
  });

  final response = await dio.post(
    url,
    data: formData,
  );
  final responseJson = json.decode(response.data);
  print(responseJson.toString());
  return response;
}

Logs

E/flutter (10772): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
E/flutter (10772): #0 signUp (package:bottlex/APIs/bottlexAPIs.dart:42:45)
E/flutter (10772):
E/flutter (10772): #1 _PersonalDataState.signUpUser (package:bottlex/Routes/personalData.dart:44:5)
E/flutter (10772): #2 _PersonalDataState.build. (package:bottlex/Routes/personalData.dart:200:23)
E/flutter (10772): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
E/flutter (10772): #4 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:568:30)
E/flutter (10772): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24)
E/flutter (10772): #6 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:246:9)
E/flutter (10772): #7 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
E/flutter (10772): #8 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:436:9)
E/flutter (10772): #9 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (10772): #10 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (10772): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:221:19)
E/flutter (10772): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:199:22)
E/flutter (10772): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (10772): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (10772): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (10772): #16 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (10772): #17 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (10772): #18 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (10772): #19 _invoke1 (dart:ui/hooks.dart:233:10)
E/flutter (10772): #20 _dispatchPointerDataPacket (dart:ui/hooks.dart:154:5)

@myacxy
Copy link

myacxy commented Mar 28, 2019

Try using explicit types. Since Flutter v1.2.1 I had to cast the result from json.decode, e.g. final Map<String, dynamic> json = jsonDecode(response.data) as Map<String, dynamic>;

@wilk3ns
Copy link
Author

wilk3ns commented Mar 28, 2019

@myacxy thank you for your suggestion, it definitely helped, however revealed main problem behind everying. Posting multipart formdata with Dio is different than dart http. I just would like to convert this code into Dio alternative

Future<Map> signUp(
    String phoneNumber, String uid, String fullName, File propic) async {
  var url = "${apiHost}api/sign_up/";

  print(
      "building request to $url and phone $phoneNumber and uid $uid and name $fullName");

  var request = new http.MultipartRequest("POST", Uri.parse(url));
  request.fields['uid'] = uid;
  request.fields['full_name'] = fullName;
  request.fields['phone'] = phoneNumber;
  if (propic != null) {
    request.files.add(new http.MultipartFile.fromBytes(
        'profile_picture', await propic.readAsBytes(),
        filename: basename(propic.path),
        contentType: new MediaType('image', 'jpeg')));
  }
  var response = await request.send();
  var respString = await response.stream.bytesToString();
  final Map<String, dynamic> responseJson =
      json.decode(respString.toString()) as Map<String, dynamic>;
  print(responseJson.toString());

  if (responseJson['result'] == 'Success') {
    var dataMap = responseJson['data'];
    token = dataMap['token'].toString();
  }
  return responseJson;
}

@wendux
Copy link
Contributor

wendux commented Mar 31, 2019

@wilk3ns Dio will decode the response body when the response content-type is "application/json" , It seems to the response.data has already be decoded to Map, please check it.

@wilk3ns
Copy link
Author

wilk3ns commented Mar 31, 2019

@wendux Thank you sir! You are my savior!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants