JoshuaaMarkk
JoshuaaMarkk

Reputation: 123

How to make HTTP GET request in flutter/dart

I am trying to make this GET request in my app but I am not sure how to go about it as I'm fairly new to flutter/dart.

I have successfully made a POST request to log in and now I am trying to make this GET request to display the messages in this message board in my app. That can come later as I am trying to complete the GET request first.

Preferably the GET request should be in a method which I can use in a button; each will be in messageBoard_Page.dart

This is the request URL that I am trying to reach.

Request URL

and this is the Request Header

Request Header

for reference this is the response that I am getting from my Login POST method

{
   "RESPONSE":{
      "login_request_result":{
         "user_must_change_password":"0"
      },
      "BASEL_RESPONSE":{
         "UserDate":"0",
         "UserTime":"0",
         "UserName":"Administrator",
         "module_config_1":"0",
         "module_config_2":"0",
         "ErrEntity":{
            "MessageID":"0",
            "last_req_id":"50029",
            "table_id":"0",
            "key_id_list":"536871",
            "operation_id":"0"
         },
         "is_csv":"0",
         "VersionName":"DYMA @ 6.1.24.0, ORG @ 2017.3.22.15.0.41, GRC @ 2017.3.22.15.0.55, LDC @ 2017.3.22.15.1.8, DYMA_XML @ 2017.3.22.15.0.30, NAS @ 2017.3.22.15.1.22 - Config: 0 - Node: OPRISK_DATACOLLECTOR",
         "ExpiryDate":"31/01/2030",
         "count_key":"0",
         "id_Us":"1",
         "is_popup":"0",
         "tot_messages":"0",
         "my_messages":"0",
         "product":"0"
      },
      "RESPONSE_HEADER":{
         "SessionID":"VtVQIdERO-206868772kpwAXF0001",
         "NomeRichiesta":"LOGIN_REQUEST",
         "ltimeStart":"22262791",
         "ltimeStop":"22262813",
         "ldate_null":"19900101",
         "product":"1",
         "server_name":"OPRISK_DATACOLLECTOR",
         "cell_context_id":"537945",
         "operation_key":"1000000",
         "operation_sub_num":"-1"
      }
   }
}

and this is my Login POST Method in login_Page.dart

void sendLogin() async {
    var map = <String, dynamic>{
      "UserName": _usernameController.text,
      "Password": _passwordController.text,
    };

    var res = await http.post(
      Uri.parse("http://192.168.1.8:8080/HongLeong/LOGIN_REQUEST.do"),
      body: map,
    );

    final data = jsonDecode(res.body);


    final String userSessionID = (data as Map)['RESPONSE']['RESPONSE_HEADER']['SessionID'];

    print(res.statusCode);
    print(res.body);


    await WriteCache.setString(key: "cache", value: userSessionID);




    if ((data as Map)['RESPONSE']['login_request_result'] == null) {
      showDialog(
        context: context,
        builder: (ctx) => AlertDialog(
          title: const Center(child: Text('Invalid Username/Password')),
          actions: <Widget>[
            Center(
              child: TextButton(
                onPressed: () {
                  _usernameController.clear();
                  _passwordController.clear();
                  Navigator.of(ctx).pop();
                },
                child: const Text('Ok'),
              ),
            ),
          ],
        ),
      );
    } else {
      Navigator.push(context,
          MaterialPageRoute(builder: (context) =>  const DashboardPage()));
    }
  }

please ignore the messiness as Im trying to achieve the GET method as of now.

Any help is appreciated. Thank You.

ps. if the images are too small, I am not sure how else to show the request URL and header, sorry.

Upvotes: 0

Views: 1867

Answers (1)

Kaushik Chandru
Kaushik Chandru

Reputation: 17822

To make a get request its very much similar to the post you created. Just remove the body and change the request type

var res = await http.get(
      Uri.parse("The url for getting the respose through get method here")
    );

Rest everything stays the same.

Upvotes: 2

Related Questions