我想用自己的api连接rest api 值

问题描述 投票:0回答:1

我可以连接rest api,但是我希望将rest api与我自己的值连接起来。这是我的代码:

var formContent = {

    "grant_type": "1",
    "branchcode": "1",
    "password": "1",
    "username": "1",
    "dbname": "1",
    "dbuser": "1",
    "dbpassword": "1",
    "dbtype": "1"

  };

Future<String> getData() async {
    http.Response response = await http.get(
        Uri.encodeFull("http://localhost:7070/api/v2/token"),
        headers: {"Accept": "application/json"});
    print(response.body);
  }

怎么能在我的getData()函数上使用我的formContent?我想将http://localhost:7070/api/v2/token与我的表单内容联系起来,我将获得令牌。

android rest dart flutter
1个回答
0
投票

要使用http请求发布数据,您必须使用post命令

var postBody= json.encode(formContent);
var resopnse = await http.post(
         Uri.encodeFull("http://localhost:7070/api/v2/token"), 
         body: postBody, 
         headers: {"Accept": "application/json"});

要导入json编码器,请导入dart:convert包

import 'dart:convert';
© www.soinside.com 2019 - 2024. All rights reserved.