Flutter:为Http GET请求发送JSON正文 [英] Flutter: Send JSON body for Http GET request

查看:393
本文介绍了Flutter:为Http GET请求发送JSON正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Flutter应用向API发出GET请求,该请求要求请求主体为JSON(原始)。

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

我使用JSON请求主体测试了API

I tested the API with JSON request body in Postman and it seems to be working fine.

现在在Flutter应用程序上,我正在尝试执行相同的操作:

Now on my Flutter application I am trying to do the same thing:

_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}');
}

但是API给我一个错误提示

However the API gives me an error saying


{消息:缺少输入json。,状态:false}

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

如何

推荐答案

,该如何在Flutter中为Http GET请求发送原始(或JSON)请求正文? uri.replace ... 返回新的 Uri ,因此您必须将其分配给新变量或直接用于 get 函数。

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"
});

使用帖子:

      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"
      });

这篇关于Flutter:为Http GET请求发送JSON正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆