Flutter POST请求正文未发送 [英] Flutter POST request body not sent

查看:92
本文介绍了Flutter POST请求正文未发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用以下代码在Flutter中发出发帖请求:

I want to make a post request in Flutter with the following code:

// 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

问题是我的发帖请求中不包含正文。我检查了服务器上的一些输出。

The problem is that my post request doesn't include the body with my request. I checked that with some outputs on my server.

推荐答案

您只需要在发送之前对主体进行编码:

You just have to encode the body before sending:

import 'dart:convert';
...

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

这篇关于Flutter POST请求正文未发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆