1

I have an API,which is written in Node.js .It is used for logging purpose.Now i'm using the same from a flutter application for performing the same Operation.

 API  :  http://192.168.137.1:5050/User/Login

How post data to the API from flutter application

Expected Input :
 { Data: { USER_NAME: 'usenmae', PASSWORD: 'password' } }

1.From Flutter

static Future<String> loginPost(String email, String password) async {
    Map<String, dynamic> jsonMap = {
      'Data': {'USER_NAME': email,'PASSWORD' : password}
    };
    try {
      final response = await http.post(
         'http://192.168.137.1:5050/User/Login',
        body: jsonMap,
      );
      return response.body;

    } catch (exception) {

    }
    return null;
  }

After calling the API by using the above code snippets i got some errors

Error : I/flutter ( 4149): send message exception called
I/flutter ( 4149): type '_InternalLinkedHashMap<String, String>' is not a subtype of type 'String' in type cast

I want pass the username and password through the api by wrapping the same with an another Data object,which look likes { Data: { USER_NAME: 'usenmae', PASSWORD: 'password' } }

Thanks

2 Answers 2

1

Before passing data to http.post(), encode the jsonMap data using dart:convert.

import 'dart:convert' as convert;

  final response = await http.post(
    'http://192.168.137.1:5050/User/Login',
    body: convert.jsonEncode(jsonMap),
    headers: {"Content-type" : "application/json"}
  );
Sign up to request clarification or add additional context in comments.

1 Comment

Tried passing data to API by encoding with jsonEncode.But passed data such as username and password not getting on the API.It return null while printing req.body on the API side in Node js. console.log(req.body); return null
0

Just need to encoded as json as said before and also put this in your header 'Content-type': 'application/json'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.