When I try the API in the server I was working fine and response also comes but when I call that post API in my flutter app then it shows the 500 internal server error. Is there is anything wrong in my code?
My API code:
Future<void> postData(Cart cart, Address selectedAddress) async {
final url = 'https://api.vezigo.in/v1/orders';
final body = {
"items": cart.items.map((cartItem) {
return {
"pack": {
"price": cartItem.price.toString(),
"unit": cartItem.package,
"quantity": cartItem.quantity.toString(),
},
"product": cartItem.name,
"quantity": cartItem.quantity
};
}).toList(),
"billAmount": cart.totalAmount.toStringAsFixed(2),
"userDetails": {
"name": selectedAddress.yourName,
"phoneNumber": selectedAddress.phoneNumber,
"altPhoneNumber": selectedAddress.alternatePhoneNumber ,
"address": "${selectedAddress.houseNumber}, ${selectedAddress.landmark}",
"notes": selectedAddress.notes
},
"geo": {
"lat": selectedAddress.latitude.toString(),
"lng": selectedAddress.longitude.toString()
}
};
final prefs = await SharedPreferences.getInstance();
final accessToken = prefs.getString('accessToken');
try {
print('Request body: ${json.encode(body)}');
print('Authorization: Bearer $accessToken');
final response = await http.post(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
},
body: json.encode(body),
);
if (response.statusCode == 200) {
print('Response data: ${response.body}');
_showOrderSuccessfulPopup(context);
} else {
// Log response for debugging
print('Error: ${response.statusCode}');
print('Response body: ${response.body}');
}
} catch (e) {
print('Exception: $e');
}
}
void _showOrderSuccessfulPopup(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Image.asset('assets/payment/tick.png'),
content: const Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Order Successful',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 30,
),
),
SizedBox(height: 10),
Text(
'Your order #986547676 has been placed successfully!',
textAlign: TextAlign.center,
),
],
),
actions: [
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.appbarColor,
padding: const EdgeInsets.symmetric(
horizontal: 50, vertical: 15)),
onPressed: () {},
child: const Text('Track my Order',
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.w800)),
),
const SizedBox(height: 18),
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
side: const BorderSide(
color:AppColors.appbarColor, width: 1),
padding: const EdgeInsets.symmetric(
horizontal: 70, vertical: 15)),
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go Back',
style: TextStyle(
color: AppColors.appbarColor,
fontWeight: FontWeight.w800)),
),
const SizedBox(height: 20)
],
),
),
],
);
},
);
}
and my print statement from API:
flutter: Request body: {"items":[{"pack":{"price":"36.0","unit":{"_id":"66d03fcc0273b734500c9eaf","quantity":1,"unit":"kg","price":36},"quantity":"1"},"product":"Aalu","quantity":1},{"pack":{"price":"100.0","unit":{"_id":"66d0400e0273b734500c9eb7","quantity":2,"unit":"kg","price":100},"quantity":"1"},"product":"Pyaaz","quantity":1},{"pack":{"price":"470.0","unit":{"_id":"66d041cc0273b734500c9ec6","quantity":3,"unit":"kg","price":470},"quantity":"1"},"product":"Fool Gobhi","quantity":1},{"pack":{"price":"1600.0","unit":{"_id":"66d045240273b734500c9ed4","quantity":5,"unit":"kg","price":1600},"quantity":"1"},"product":"Broccoli","quantity":1}],"billAmount":"2206.00","userDetails":{"name":"TAnu","phoneNumber":"9602046633","altPhoneNumber":"9602046633","address":"Basni, Basni","notes":"hello"},"geo":{"lat":"26.21638011850468","lng":"73.01504015922548"}} flutter: Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2NmVkMmRhMjliZGZlMTAwNmUzOTY5YWQiLCJpYXQiOjE3MjY4MTk3NzEsImV4cCI6MTcyNjgyMTU3MSwidHlwZSI6ImFjY2VzcyJ9.3Pg_GNqOv4jvdgl62xSX1ljlJSnCafs1HWEvtkEunv0 flutter: Error: 500 flutter: Response body: {"code":500,"message":"Internal Server Error","data":{}}
Here are some suggestions:
If this still not work let me know.