【问题标题】:send http form GET in dart在 dart 中发送 http 表单 GET
【发布时间】:2021-08-12 06:00:03
【问题描述】:

我正在寻找一种能够在 dart 中以 Json 样式发送 GET HTTP 请求的方法。我到处发现人们在使用 POST 时遇到问题,但没有关于 GET 的文档。问题是,如果我写 http.get(Uri.parse(uri)) 我不能把正文:(表格会去哪里)。它只允许我在 POST 时写正文。 我希望能够使用 GET 运行下一个代码。 Uri.parse(我不知道为什么,但它是让我获取 de URL 的唯一方法)

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  postTest() async {
    final uri = 'http://10.0.2.2:5000/qna/';

    http.Response response = await http.post(Uri.parse(uri), body: {
      'pregunta': 'contacto',
    });

    print(response.body);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter WebView '),
        ),
        body: Container(
          child: TextButton(onPressed: postTest, child: Text('Push me')),
        ),
      ),
    );
  }
}

【问题讨论】:

  • get 方法仅支持两个参数:urlheaders。你可以在Official documentation找到完整的方法描述。
  • 您不能将正文放在 GET 中,因为GET is used only to retrieve remote data, and not to insert
  • 对不起,我是个新手……我正在学习:)。但我有一个答案...如果我想登录,这意味着我需要将其作为 POST 请求进行吗?因为我不能在 GET 请求中发送表单......但是每个移动应用程序都使用 POST 吗?我很困惑

标签: flutter dart


【解决方案1】:

您不能在获取请求中添加正文。但您可以在查询参数中传递数据。 像这样

postTest() async {
    final uri = 'http://10.0.2.2:5000/qna?pregunta=contacto';

    http.Response response = await http.get(Uri.parse(uri));

    print(response.body);
  }

【讨论】:

  • 对不起,我是个新手……我正在学习:)。但我有一个答案......如果我想登录,这意味着我需要将其作为 POST 请求进行吗?因为我不能在 GET 请求中发送表单......但是每个移动应用程序都使用 POST 吗?我很困惑
  • 你必须使用post登录。
猜你喜欢
  • 2021-06-05
  • 2022-01-18
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多