[英]Dart: How to make GET request with body
How to send GET request with body in dart?如何在 dart 中发送带有正文的 GET 请求?
I tried all recipes from SO ( this and that and more) but with no success.我尝试了 SO 的所有食谱( 这个和那个等等),但没有成功。
final queryParameters = {
"id": "6767676767676",
"device": "tatatata",
"user": {
"login": "login",
"password": "pwd",
}
};
This is complex JSON and i got an error when send request: final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);
这是复杂的 JSON 发送请求时出现错误: final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);
Exception: type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>'
I think your queryParameters
should not contain objects.我认为您的queryParameters
不应包含对象。
final queryParameters = {
"id": "6767676767676",
"device": "tatatata",
"login" : "login",
"password" : "password",
};
have you tried:你有没有尝试过:
var response = await get(url);
var fetchedData = json.decode(response.body);
fetchedData will be in a json map format fetchedData 将采用 json map 格式
The answer its actually what Flutter has suggested in this part of exception message
:答案实际上是 Flutter 在这部分exception message
中建议的:
'_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Map<String, String>' “_InternalLinkedHashMap<String, Object>”不是“Map<String, String>”类型的子类型
It means that, one of your variable is '_InternalLinkedHashMap<String, Object>'
and flutter wants you to change it to 'Map<String, String>'
type这意味着,您的变量之一是'_InternalLinkedHashMap<String, Object>'
并且 flutter 希望您将其更改为'Map<String, String>'
类型
You should make one of your key in queryParameters
to this 'Map<String, String>'
type您应该将queryParameters
中的键之一设为此'Map<String, String>'
类型
by importing dart:convert
, we will have jsonEncode
method通过导入dart:convert
,我们将有jsonEncode
方法
import 'dart:convert'; // put this at top of your file
final nestedValue = {
"login": "login",
"password": "pwd",
};
String stringValue = jsonEncode(nestedValue);
final queryParameters = {
"id": "6767676767676",
"device": "tatatata",
"user": stringValue
};
final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);
This will be your final code looks like below:这将是您的最终代码,如下所示:
import 'dart:convert'; // put this at top of your file
final nestedValue = {
"login": "login",
"password": "pwd",
};
String stringValue = jsonEncode(nestedValue); // add this
final queryParameters = {
"id": "6767676767676",
"device": "tatatata",
"user": stringValue
};
final uri = Uri.http('44.44.444.444:8080', 'get-answer', queryParameters);
I advice you to use the http package it is perfect for performing http requests.我建议您使用http package它非常适合执行 http 请求。 before sending data make sure you encode you map into json format using the jsonEncode
function and to obtain the data from a GET
request decode the body of the request from json format using the jsonDecode function. before sending data make sure you encode you map into json format using the jsonEncode
function and to obtain the data from a GET
request decode the body of the request from json format using the jsonDecode function.
dart:convert
package这些函数是从dart:convert
package 导入的//import the packages
import 'package:http/http.dart' as http;
import 'dart:convert';
//other code here
final queryParameters = jsonEncode( {
"id": "6767676767676",
"device": "tatatata",
"login" : "login",
"password" : "password",
});
//we send the data to the server with the post method
await http.post("44.44.444.444:8080",queryParameters);
similarly for retrieving data:
final res = http.get("getUrl");
//if the request is successful we get the data
if(res.statusCode == 200)
final data = jsonDecode(res.body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.