我想使用以下代码在 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
Run Code Online (Sandbox Code Playgroud)
问题是我的帖子请求不包括我的请求正文。我用服务器上的一些输出检查了这一点。
您只需要在发送之前对正文进行编码:
import 'dart:convert';
...
var bodyEncoded = json.encode(body);
var response = await http.post(url, body: bodyEncoded , headers: {
"Accept": "application/json"
},);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6032 次 |
| 最近记录: |