Flutter:为 Http GET 请求发送 JSON 正文

cod*_*ess 13 json dart flutter

我需要从我的 Flutter 应用程序向 API 发出 GET 请求,该请求需要请求正文为 JSON(原始)。

我在 Postman 中使用 JSON 请求正文测试了 API,它似乎工作正常。

在此处输入图片说明

现在在我的 Flutter 应用程序中,我正在尝试做同样的事情:

_fetchDoctorAvailability() async {
    var params = {
      "doctor_id": "DOC000506",
      "date_range": "25/03/2019-25/03/2019" ,
      "clinic_id":"LAD000404"
    };

    Uri uri = Uri.parse("http://theapiiamcalling:8000");
    uri.replace(queryParameters: params);

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

    print('---- status code: ${response.statusCode}');
    var jsonData = json.decode(response.body);

    print('---- slot: ${jsonData}');
}
Run Code Online (Sandbox Code Playgroud)

但是 API 给了我一个错误说

{message: Missing input json., status: false}

如何在 Flutter 中为 Http GET 请求发送原始(或者更确切地说是 JSON)请求正文?

Sur*_*gch 11

得到

GET 请求并非用于向服务器发送数据(但请参阅此)。这就是该http.dart get方法没有body参数的原因。但是,当您想要指定从服务器获取的内容时,有时您需要包含查询参数,这是一种数据形式。查询参数是键值对,因此您可以将它们作为映射包含,如下所示:

final queryParameters = {
  'name': 'Bob',
  'age': '87',
};
final uri = Uri.http('www.example.com', '/path', queryParameters);
final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
final response = await http.get(uri, headers: headers);
Run Code Online (Sandbox Code Playgroud)

邮政

不像GET请求,POST请求用于在体内发送数据。你可以这样做:

final body = {
  'name': 'Bob',
  'age': '87',
};
final jsonString = json.encode(body);
final uri = Uri.http('www.example.com', '/path');
final headers = {HttpHeaders.contentTypeHeader: 'application/json'};
final response = await http.post(uri, headers: headers, body: jsonString);
Run Code Online (Sandbox Code Playgroud)

请注意,参数是 Dart 侧的 Map。然后它们被库中的json.encode()函数转换为 JSON 字符串dart:convert。该字符串是 POST 正文。

因此,如果服务器要求您在 GET 请求正文中传递数据,请再次检查。虽然可以以这种方式设计服务器,但它不是标准的。


die*_*per 10

uri.replace...返回一个 new Uri,因此您必须将其分配给一个新变量或直接在get函数中使用。

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"
});
Run Code Online (Sandbox Code Playgroud)

使用帖子:

      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"
      });
Run Code Online (Sandbox Code Playgroud)


Has*_*awy 5

您可以使用Request类,如下所示:

var request = http.Request(
  'GET',
  Uri.parse("http://theapiiamcalling:8000"),
)..headers.addAll({
    "Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY,
    HttpHeaders.contentTypeHeader: "application/json",
    "callMethod": "DOCTOR_AVAILABILITY",
  });
var params = {
  "doctor_id": "DOC000506",
  "date_range": "25/03/2019-25/03/2019",
  "clinic_id": "LAD000404"
};
request.body = jsonEncode(params);
http.StreamedResponse response = await request.send();
print(response.statusCode);
print(await response.stream.bytesToString());
Run Code Online (Sandbox Code Playgroud)

另请注意,Postman 可以将 API 请求转换为超过 15 种语言的代码片段。如果你选择Dart,你会发现与上面类似的代码。

  • 这应该是公认的答案。Elastic search 使用 GET body 请求。看到人们试图告诉你使用参数而不是回答问题,这是一个巨大的痛苦 (2认同)