Flutter 颤振-如何在Dart中使用HTTP发送POST请求?

Flutter 颤振-如何在Dart中使用HTTP发送POST请求?,flutter,http,dart,post,Flutter,Http,Dart,Post,我正在使用一个API将HTTP转换为JSON,并从服务器获取响应,我需要发送一个HTML post请求,但我不知道如何做到这一点 这是我当前的实现- Future<String> test() async { var link = await http.get('https://example.com'); var body = parse(link.body); return body.outerHtml; } Future<Album> cr

我正在使用一个API将HTTP转换为JSON,并从服务器获取响应,我需要发送一个HTML post请求,但我不知道如何做到这一点

这是我当前的实现-

Future<String> test() async {
  var link =
      await http.get('https://example.com');
  var body = parse(link.body);
  return body.outerHtml;
}

Future<Album> createAlbum(String html) async {
  final http.Response response = await http.post(
    'https://www.html2json.com/api/v1',
    headers: <String, String>{
      'Content-Type': 'text/html; charset=UTF-8',
    },
  
    body: html,
  );

  if (response.statusCode == 200) {
    return Album.fromJson(jsonDecode(response.body));
  } else {
    throw Exception('Failed to create album.');
  }
}

检查了以下对我有效的代码

Future<Album> createAlbum(String html) async {
  final http.Response response = await http.post(
    'https://www.html2json.com/api/v1',
    headers: <String, String>{
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    },
  
    body: html,
  );
Future createAlbum(字符串html)异步{
final http.Response Response=wait http.post(
'https://www.html2json.com/api/v1',
标题:{
“内容类型”:“应用程序/x-www-form-urlencoded;字符集=UTF-8”,
},
正文:html,
);

您需要将内容类型更改为
application/x-www-form-urlencoded;charset=UTF-8
。这应该可以解决问题。

多亏了DARSH SHAH,我用dio()解决了这个问题

动态响应;
Dio Dio=Dio();
Future test()异步{
动态链接=等待dio.get(
'https://example.com');
返回链接;
}
Future post(动态主体)异步{
字符串_baseUrl=”https://html2json.com/api/v1";
var选项=选项(
contentType:“application/x-www-form-urlencoded;charset=UTF-8”,
followRedirects:false,
);
最终响应=等待dio.post(
_baseUrl,
资料来源:body,
选项:选项,
);
返回响应;
}
浮动操作按钮(
onPressed:()异步{
动态响应HTML=等待测试();
response=wait post(responseHTML.data);//这将包含JSON响应
},
);

你能给我发送你想要解析的html吗?我在google.com上也遇到了同样的错误,你能试试吗?我已经为你制作了一个视频。看看。你能在github上分享你的全部代码吗?也许我遗漏了一些东西。当然,我必须使用dio。对于请求处理,你可以使用任何你想要的东西。
Future<Album> createAlbum(String html) async {
  final http.Response response = await http.post(
    'https://www.html2json.com/api/v1',
    headers: <String, String>{
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    },
  
    body: html,
  );
dynamic response;

  Dio dio = Dio();
  Future<dynamic> test() async {
    dynamic link = await dio.get(
        'https://example.com');
    return link;
  }

  Future<dynamic> post(dynamic body) async {
    String _baseUrl = "https://html2json.com/api/v1";
    var options = Options(
      contentType: "application/x-www-form-urlencoded; charset=UTF-8",
      followRedirects: false,
    );

    final response = await dio.post(
      _baseUrl,
      data: body,
      options: options,
    );
    return response;
  }

FloatingActionButton(
        onPressed: () async {
          dynamic responseHTML = await test();
          response = await post(responseHTML.data); //This will contain the JSON response
        },
);