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

编程入门 行业动态 更新时间:2024-10-15 02:27:42
本文介绍了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("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("theapiiamcalling:8000", body: json.encode(params) ,headers: { "Authorization": Constants.APPOINTMENT_TEST_AUTHORIZATION_KEY, HttpHeaders.contentTypeHeader: "application/json", "callMethod" : "DOCTOR_AVAILABILITY" });

更多推荐

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

本文发布于:2023-11-26 14:02:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634074.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正文   Flutter   Http   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!