anlat_
Server returns a 400 Bad Request error.
flutter main.dart
Future<void> _register() async {
try {
final response = await http.post(
Uri.parse('https://wk.up.railway.app/dj-rest-auth/registration/'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'username': _usernameController.text,
'email': _emailController.text,
'password1': _passwordController.text,
'password2': _confirmPasswordController.text,
}),
);
if (response.statusCode == 201) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Registration successful!'),
backgroundColor: Colors.green,
));
// Navigate to Home Page after a short delay to let the SnackBar show
Future.delayed(Duration(seconds: 2), () {
Navigator.pop(context);
});
} else {
print('Response body: ${response.body}');
if (response.body.isNotEmpty) {
// Only attempt to decode if the body is not empty
Map<String, dynamic> responseBody = json.decode(response.body);
String error = responseBody.values.first[0];
throw Exception(error);
} else {
throw Exception('Registration failed without a specific reason.');
}
}
11 replies