Search code examples
mongodbapiflutterdio

Data isn't sending properly in dio , flutter


I am trying to pass token and username which I pass from login screen using sharedPreference in data, my api is working properly on postman it response successfully according to my requirement but it's still giving me Error on response.data.

here is my update code

Dio dio=new Dio();
    var data={
      'username': getname,
      'token': getaccesstoken
    };
    await dio
    .post(localhostUrlTimeIn,data: json.encode(data))
      .then((onResponse)  async {
       
        print(onResponse.headers);
        print(onResponse.statusCode);
        print(onResponse.data);
      }).catchError((onerror){
        print(onerror.toString());
        //showAlertDialog(context);
    });

here are the logs

I/flutter (17731): 1
I/flutter (17731): x-powered-by: Express
I/flutter (17731): connection: keep-alive
I/flutter (17731): keep-alive: timeout=5
I/flutter (17731): date: Wed, 09 Jun 2021 21:07:41 GMT
I/flutter (17731): content-length: 7
I/flutter (17731): etag: W/"7-Vuu5vA8hV5HSudFEr8bWQajjaE0"
I/flutter (17731): content-type: application/json; charset=utf-8
I/flutter (17731): 200
I/flutter (17731): Error

----------------------------------UPDATED ------------------------

here is the backend code

TimeIn=(req,res)=>{
    jwt.verify(req.body.token, 'secret' , function(err, decoded) {
        if (err) 
        {
         err["expiredAt"] = err["expiredAt"].toLocaleString();   
         res.status(300).json(err)
        }else{
        
      let today = new Date() 
      Today = today.toLocaleString();
      var date = Today.split(",")
      var  document = new User();
      
      User.find({"username":req.body.username},function(err,data){
      var dat = date[0];
      var da = dateformat(dat,"yyyy-mm-dd")
      console.log(da);
      document.username= data[0].username;
      document.Date = da;
      document.TimeIn = date[1];
      document.TimeOut = "";
      document.manager_id= data[0].manager_id,
      document.code = data[0].code
      document.save();
      console.log(document);
      
      var token = jwt.sign({
        data: 'foobar'
    }, 'secret', { expiresIn: "30 minute"})
    
      res.status(200).json({auth: true, AccessToken: token})
      })
    }
    })
}

and here i call above method in app.js file

const  TimeIn = require('./routes/TimeInTimeOut')
app.post("/TimeIn",checkToken,function(req,res){
  console.log("api hit")
  TimeIn.TimeIn(req,res)    
}) 

and here is where i am checking token,which is created in app.js file!!

function checkToken(req,res,result){
  const header= req.body.token;
  if(typeof header !== 'undefined'){
    const bearer =header.split('.');
    const token = bearer[1]
    //console.log(token)
    req.token = token 
    //next();
    result();
  }else
  res.json("Error")
  }  

i am getting now this error in frontend

 DioError [DioErrorType.response]: Http status error [300]

here is the postman output enter image description here

and here is the backend output enter image description here

Please help me, i have tried it too much but still getting error.


Solution

  • Your response is fully working. You do get data back and a 200 status code. This means the request was a succes. You are assigning error to the data in the request serverside. The issue isn't in your request in the client.

    // headers
    I/flutter (17731): 1
    I/flutter (17731): x-powered-by: Express
    I/flutter (17731): connection: keep-alive
    I/flutter (17731): keep-alive: timeout=5
    I/flutter (17731): date: Wed, 09 Jun 2021 21:07:41 GMT
    I/flutter (17731): content-length: 7
    I/flutter (17731): etag: W/"7-Vuu5vA8hV5HSudFEr8bWQajjaE0"
    I/flutter (17731): content-type: application/json; charset=utf-8
    //Statuscode
    I/flutter (17731): 200
    //data
    I/flutter (17731): Error