Flutter app eken http post req ekak send karahama 500 error ekak enavane . postman eken API eka test kra hodata weda okkoma methoda
-
(
mage node.js api eka post man eken test karama hodata weda get, post , put request okkoma check kra. mongoose ekka ynne . post eken data idan insert wenawa kisi aulak nethuva postman eken test karaddi. but flutter android app ekaen api eka haraha data send kranna hedhuvama 500 internal error ekak enavane. fix kranne komada meka ?
http.dart file
import 'dart:convert'; import 'package:http/http.dart' as http; class RequestResult { bool ok; dynamic data; RequestResult(this.ok, this.data); } const PROTOCOL = 'http'; const DOMAIN = "192.168.*.**:3000"; //const ROUTE = "users/signup"; Future<RequestResult> http_post(String route, [dynamic data]) async { var url = "$PROTOCOL://$DOMAIN/$route"; var dataStr = jsonEncode(data); var result = await http.post(url, body: dataStr, headers:{"Content_Type": "application/json"}); return RequestResult(true, jsonDecode(result.body)); }
signup.dart file
class _SignUpState extends State<SignUp> { TextEditingController userNameController = TextEditingController(); TextEditingController emailController = TextEditingController(); TextEditingController phoneNumberController = TextEditingController(); TextEditingController nicController = TextEditingController(); TextEditingController passwordController = TextEditingController(); String response = ""; createUser() async { var result = await http_post("users/signup", { "userName": userNameController.text, "email": emailController.text, "phoneNumber": phoneNumberController.text, "nic": nicController.text, "password": passwordController.text, }); if(result.ok) { setState(() { response = result.data['status']; }); } }
Api app.js code headers middlewaes
mongoose.Promise = global.Promise; //middlewares app.use(morgan('dev')); app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.json()); //set access to all connect to this api app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'X-Requested-With, Origin, Content-Type, Accept, Authorization'); next(); }); app.use('/users', usersRoutes); app.use('/posts', postsRoutes); app.use((req, res, next) =>{ const error = new error('Not found'); error.status= 404; next(error); }) app.use((error, req, res, next) => { res.status(error.status || 500); res.json({ error: { message: error.message } }); }); module.exports = app;
-
500 ennenam API eke error ekak nisa, app eken request eka send karama blnna request parameters api ekata hriyta receive wenwda kyla
-
@dev_lak eka komada balanne postman eken check kraddiapieka wedane
-
@Nisal-Vimukthi postman eken parameters x-form-urlencoded karalada yawwe?
-
@dev_lak raw data json format eken yewwe
-
@Nisal-Vimukthi app ekenut raw format eken ywnna one bro.. natnm api ekt gnne nha parameters
-
@dev_lak ah flutter walin komada yawanne ? mn me 1st time karanne ekai wediya dan ne :drooling_face:
-
@Nisal-Vimukthi bro https://stackoverflow.com/questions/50278258/http-post-with-json-on-body-flutter-dart/50295533 meke answer eka balannna
-
@dev_lak thankx bro goda :blush:
-
@Nisal-Vimukthi elaaa