小编Fir*_*rat的帖子

如何在 Flutter 中将 cURL 转换为 http 帖子

我有一个 Rest API 成功运行,并且可以将 curl 发布为:

curl -X POST "{{baseURL}}/api/auth/login" -H "accept: application/json" -H "Content-Type: application/json-patch+json" -d "{ \"nick\": \"string"\", \"password\": \"string\"}"
Run Code Online (Sandbox Code Playgroud)

我的愿望是编写一个代码来完成上述命令的确切工作,我的意思是如何以正确的方式解码/编码东西。这是我到目前为止得到的:

Future<http.Response> postRequest (String nick, String password) async {
  var url ='{{baseURL}}/api/auth/login';
  var body = jsonEncode({ "nick": "$nick", "password": "$password"});

  print("Body: " + body);

  http.post(url,
    headers: {"accept": "application/json","Content-Type": "application/json- 
     patch+json"},
    body: body
  ).then((http.Response response) {
 });
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

curl dart flutter

5
推荐指数
1
解决办法
5568
查看次数

标签 统计

curl ×1

dart ×1

flutter ×1