gpt4 book ai didi

dart - 未发送 Flutter POST 请求正文

转载 作者:IT王子 更新时间:2023-10-29 06:40:25 25 4
gpt4 key购买 nike

我想用下面的代码在 Flutter 中发出一个 post 请求:

// Body: {"email": "example@email.com", "pass": "passw0rd"}

Future<dynamic> post(String url, var body) async {
var response = await http.post(url, body: body);
final String res = response.body;
return res;
}

// That's not the full code. I removed some lines because they are useless for this thread.
// Most of them are only some debug outputs or conditional statements

问题是我的发帖请求没有包含请求的正文。我用服务器上的一些输出检查了这一点。

最佳答案

您只需在发送前对正文进行编码:

import 'dart:convert';
...

var bodyEncoded = json.encode(body);
var response = await http.post(url, body: bodyEncoded , headers: {
"Content-Type": "application/json"
},);

关于dart - 未发送 Flutter POST 请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53792081/

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