gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:48:02 26 4
gpt4 key购买 nike

我正在使用将 HTTP 转换为 JSON 的 API 并从服务器获取响应,我需要发送 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.');
}
}
我称这是我的应用程序启动的时候,
@ovveride 
void initState() {
test().then((body) => createAlbum(body)); //Output returns HTTP error 301, am I doing something wrong?
super.initState();
}

最佳答案

检查了以下对我有用的代码。

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,
);
您需要将 Content-type 更改为 application/x-www-form-urlencoded; charset=UTF-8 .这应该可以解决问题。

关于Flutter - 如何在 Dart 中使用 HTTP 发送 POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64305922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com