I am trying to communicate with a PHP backend using API but I can not reach the body of the response.
I got the base code from the postman. And here is the data of the body response:
I need to reach the message, and the errors to show them in the UI, the problem is response.stream
it's type is Bytestreem and I can not convert it to Map
My code:
Future<void> _authenticateUp(String email, String password, String passwordconfirmation, String username, String name, {String phonenumber}) async { var headers = { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }; var request = http.MultipartRequest('POST', Uri.parse('$siteUrl/register')); request.fields.addAll({ 'email': email, 'password': password, 'password_confirmation': passwordconfirmation, 'username': username, 'name': name, 'phone_number': phonenumber }); request.headers.addAll(headers); http.StreamedResponse response = await request.send(); try { if (response.statusCode == 200) { await response.stream.bytesToString().then((value) { print(value); }); } else { // here I want to print the message and the errors } } catch (e) { throw e; } }
https://stackoverflow.com/questions/67351665/fetching-the-response-from-api-and-dealing-with-the-errors-converting-bytestre May 02, 2021 at 07:18AM
没有评论:
发表评论