在扑扑中发送带有JSON正文的GET请求

时间:2020-08-20 15:09:47

标签: json api http flutter dart

我需要从我的flutter应用程序向api发送一个GET请求,并在主体中使用json参数。发送带有json正文的GET请求。 当我在POSTMAN中尝试该请求时,它就会起作用。

var url = 'linkofapi;


var response = await http.get(url);

enter image description here

2 个答案:

答案 0 :(得分:0)

尝试一下

Future<http.Response> createUser({name, email, password, gender, dob}) {
  return http.get(
    'http://$BASE_API_URL/users',
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      'name': name,
      'email': email,
      'password': password,
      'gender': gender,
      'dob': dob,
    }),
  );
}

答案 1 :(得分:0)

首先,请参阅GET操作并非旨在发送数据。它旨在接收数据,因此方法名称为GET,因此http.get()没有body

如果最需要发送数据,则需要使用Uriquery parameter方法中添加http

请阅读有关Uri.http抖动的详细信息。

您可以执行以下操作来实现所需的目标:

// Please note "...." for more information, please do not use this as is
// Just wanted to give a heads up
final query = {
  'name': your_name,
  'email': your_email,
  'password': your_password
  ...
};

var url = 'linkofapi;

// Now you use the query to pass it to. your get method
final uri = Uri.http(url, '/path', query);
// adding headers to the query
final header = {HttpHeaders.contentTypeHeader: 'application/json'};
// doing the operation finally
final response = await http.get(uri, headers: header);
相关问题