I am getting this error in my flutter app during the api integration phase. I have checked the backend apis through postman they are working fine. But when i integrate it with frontend and then check it gives 404 Not Found,
Future<Map<String, dynamic>> registerUser(
String username, String email, String password) async {
var baseUrl = 'http://localhost:52961/api/v1/user/';
final url = Uri.parse('${baseUrl}register');
_logger
.d('Sending request to $url with email: $email, username: $username');
try {
var response = await http.post(url,
body: jsonEncode({
'username': username,
'email': email,
'password': password,
}));
** _logger.d('Received response with status code: ${response.statusCode}');
_logger.d('Response body: ${response.body}');**
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
await storage.write(key: 'accessToken', value: data['accessToken']);
await storage.write(key: 'refreshToken', value: data['refreshToken']);
_logger.i('User registered successfully');
return {'statusCode': 200, 'message': 'User registered successfully'};
} else {
final errorData = jsonDecode(response.body);
_logger.e('Failed to register user: ${errorData['message']}');
return {
'statusCode': response.statusCode,
'message': errorData['message'] ?? 'Failed to register user'
};
}
} catch (e) {
_logger.e('An error occurred: $e');
return {'statusCode': 500, 'message': 'An error occurred'};
}
}
this is my backend api code in Node.js Express. Kindly help me out because im a noob xd
i tried gpt but it tells me to check endpoints which i have for hundred times. im lost
2
Answers
The url is to a localhost connection. The frontend you are talking about, is it also connected to the same localhost server? For example, if it is an external device like a phone, it will surely not work.
if you are on localhost make sure you are connected to the same router and android will not allow an HTTP connection so add
to your manifest. also, your PC’s firewall will block certain connections, so make sure you have an exposed 8080 port on your router. try this if it works.