Flutter: Send JSON body for Http GET request

后端 未结 2 482
借酒劲吻你
借酒劲吻你 2020-12-19 04:44

I need to make a GET request to an API from my Flutter app which requires request body as JSON (raw).

I tested the API with JSON request body in Postman and it seems

2条回答
  •  清歌不尽
    2020-12-19 05:24

    uri.replace... returns a new Uri, so you have to assign it into a new variable or use directly into the get function.

    final newURI = uri.replace(queryParameters: params);
    
    var response = await http.get(newURI, headers: {
      "Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
      HttpHeaders.contentTypeHeader: "application/json",
      "callMethod" : "DOCTOR_AVAILABILITY"
    });
    

    using post:

          var params = {
            "doctor_id": "DOC000506",
            "date_range": "25/03/2019-25/03/2019" ,
            "clinic_id":"LAD000404"
          };
    
          var response = await http.post("http://theapiiamcalling:8000", 
          body: json.encode(params)
          ,headers: {
            "Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
            HttpHeaders.contentTypeHeader: "application/json",
            "callMethod" : "DOCTOR_AVAILABILITY"
          });
    

提交回复
热议问题