Home > Mobile >  How do I use the response header from a POST request to make a new GET request? Flutter/Dart/HTTP
How do I use the response header from a POST request to make a new GET request? Flutter/Dart/HTTP

Time:07-18

I have made a POST request and received a response body and header. Now how do I use the header to make a GET request?

This is the code for the POST Login Request.

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);
    
    print(res.statusCode);
    print(res.body);
    print(res.headers);
    
    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()));
    }
  }

This is the response body :

{
   "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":"wVBeuYoPs1531497370SOhUMF0001",
         "NomeRichiesta":"LOGIN_REQUEST",
         "ltimeStart":"15314968",
         "ltimeStop":"15314986",
         "ldate_null":"19900101",
         "product":"1",
         "server_name":"OPRISK_DATACOLLECTOR",
         "cell_context_id":"537554",
         "operation_key":"1000000",
         "operation_sub_num":"-1"
      }
   }
}

This is the response header :

{set-cookie: JSESSIONID=7E9295D8288C42221B1AF7935EF60EB4; Path=/HongLeong; HttpOnly,OpRiskSessionID=wVBeuYoPs1531497370SOhUMF0001, transfer-encoding: chunked, date: Wed, 13 Jul 2022 07:31:49 GMT, vary: Accept-Encoding,Accept-Encoding, content-encoding: gzip, content-type: text/html;charset=UTF-8, server: Apache-Coyote/1.1}

and finally this is the code to make the GET request :

void getMessageBoard() async {

    final response = await http.get(
      Uri.parse("http://192.168.1.8:8080/HongLeong/MENU_REQUEST.do?_dc=1657668533900&table_id=25018&id_MenuAction=3&reset_context=1&ViewType=MENU_REQUEST&gui_open_popup=1&id_Window=2&activeWindowId=mw_2&noOrigUserDate=true&LocalDate=20220713&LocalTime=07285300&TimeZone=Asia/Shanghai&UserDate=0&UserTime=0&server_name=OPRISK_DATACOLLECTOR&key_id_list=&cell_context_id=0&id_Desktop=100231&operation_key=1000013&operation_sub_num=-1&is_json=1&is_popup=0&is_search_window=0&ccsfw_conf_by_user=0&is_batch=0&previousToken=1657668514164&historyToken=1657668533898&historyUrl=1"),
    );

    print(response.body);
  }

From here on in another page, how do I use the header or body to make a GET request? Thanks.

CodePudding user response:

To append some of your custom headers to your GET request you can that call method somehow like this.

var response = await http.get(url, headers: headers);

Where headers will be your custom parsed map e.g.:

 Map<String, String> headers = {
       'Content-type': 'application/json',
       'Accept': 'application/json',
       'Authorization': '$token',
       ...
     };
  • Related