如何在flutter中使用curl执行https发布请求

时间:2019-09-25 11:17:04

标签: http curl flutter

我是不熟悉curl的人,我正尝试使用curl执行https发布请求。而且它似乎不像其他json发布请求那样工作,因此我希望有人可以向我解释一下

2 个答案:

答案 0 :(得分:0)

我不确定我是否足够理解您的答案,但是您可以看一下Packagethis one,后者提供了更多的灵活性和功能,例如拦截器等。

答案 1 :(得分:0)

您可以使用此软件包Curl https://pub.dev/packages/curl

示例代码

import 'package:curl/curl.dart';
import 'package:http/http.dart';

final req1 = new Request("GET", "https://exyui.com/endpoint");
print(toCurl(req1));
// will print out:
// curl 'https://exyui.com/endpoint' --compressed --insecure

final req2 = new Request("PUT", "https://exyui.com/endpoint");
req2.body = "This is the text of body?, \\, \\\\, \\\\\\";
print(req2);
// will print out:
// curl 'https://exyui.com/endpoint' -X PUT -H 'content-type: text/plain; charset=utf-8' --data-binary \$'This is the text of body\\ud83d\\ude05, \\, \\\\, \\\\\\' --compressed --insecure

final req3 = new Request("POST", "https://exyui.com/endpoint");
final part1 = "This is the part one of content";
final part2 = "This is the part two of content?";
final expectQuery = "part1=This%20is%20the%20part%20one%20of%20content&part2=This%20is%20the%20part%20two%20of%20content%F0%9F%98%85";
req3.bodyFields = {
"part1": part1,
"part2": part2,
};
print(toCurl(req3));