Home > Blockchain >  Send header in HTTP post request in flutter
Send header in HTTP post request in flutter

Time:10-09

first of all i have LoginPageModel to run HTTP post request like this

class LoginPageModel {
  String CODD_VALU;

  String CODD_DESC;

  LoginPageModel({required this.CODD_VALU, required this.CODD_DESC});

  static Future<LoginPageModel> connect(String CODD_VALU) async {
    Uri url = Uri.parse("http://deltaprima.rmdoo.com/api/office/all");

    var response = await http.post(
      url,
      headers: {
        "CompanyCode": "MW",
      },
    );

    var dataJson = jsonDecode(response.body);
    debugPrint(dataJson);

    return LoginPageModel(
      CODD_VALU: dataJson["CODD_VALU"],
      CODD_DESC: dataJson["CODD_DESC"],
    );
  }
}

Then i run LoginPageModel on initState class. Like this

void initState() {
    super.initState();
  
    LoginPageModel.connect("MW").then((value) {
      print(value);
    });
  }

But i cant get the Print of the value on my debugConsole, and the "CompanyCode" must send trough headers. im also trying postman, it worked and get the data from api like this.

[
    {
        "CODD_DESC": "DELTA PRIMA",
        "CODD_VALU": "01"
    }
]

But i cant get data from API using my flutter app. How

CodePudding user response:

If using post is not strictly necessary, you can use get instead.

      String url ='http://deltaprima.rmdoo.com/api/office/all';
      var response = await http.get(Uri.parse(url), 
      headers: {
      "CompanyCode": "MW",
       });

Moreover, please check if you're using get or post method in postman.

  • Related