如何在flutter中使用url编码的正文发出HTTP POST请求?

编程入门 行业动态 更新时间:2024-10-25 13:28:05
本文介绍了如何在flutter中使用url编码的正文发出HTTP POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试以 url 编码的内容类型在 flutter 中发出 post 请求.当我编写 body : json.encode(data) 时,它会编码为纯文本.

I'm trying to make an post request in flutter with content type as url encoded. When I write body : json.encode(data), it encodes to plain text.

如果我写 body: data 我得到错误 type '_InternalLinkedHashMap'不是类型转换中String"类型的子类型

If I write body: data I get the error type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' in type cast

这是数据对象

var match = { "homeTeam": {"team": "Team A"}, "awayTeam": {"team": "Team B"} };

还有我的要求

var response = await post(Uri.parse(url), headers: { "Accept": "application/json", "Content-Type": "application/x-www-form-urlencoded" }, body: match, encoding: Encoding.getByName("utf-8"));

推荐答案

您需要添加三个额外的步骤:首先,您需要将 JSON 映射转换为字符串(使用 json.encode)然后,如果要将其作为 application/x-www-form-urlencoded 发送,则需要对其进行 Uri 编码.最后,您需要为要发布的参数指定名称.

You need to add three additional steps: First, you need to convert the JSON map to a String (using json.encode) Then you need to Uri encode it if you want to send it as application/x-www-form-urlencoded. Lastly, you need to give the parameter that you are posting a name.

例如(注意,这里使用的是dart:io HttpClient,但基本相同):

For example (note, this is using the dart:io HttpClient, but it's basically the same):

Future<HttpClientResponse> foo() async { Map<String, dynamic> jsonMap = { 'homeTeam': {'team': 'Team A'}, 'awayTeam': {'team': 'Team B'}, }; String jsonString = json.encode(jsonMap); // encode map to json String paramName = 'param'; // give the post param a name String formBody = paramName + '=' + Uri.encodeQueryComponent(jsonString); List<int> bodyBytes = utf8.encode(formBody); // utf8 encode HttpClientRequest request = await _httpClient.post(_host, _port, '/a/b/c'); // it's polite to send the body length to the server request.headers.set('Content-Length', bodyBytes.length.toString()); // todo add other headers here request.add(bodyBytes); return await request.close(); }

以上是针对dart:io版本的(当然,你可以在Flutter中使用)如果您想坚持使用 package:http 版本,那么您需要稍微调整一下您的 Map.body 必须是 Map.你需要决定你想要什么作为你的 POST 参数.你想要两个:homeTeam 和 awayTeam?或者一个,比如说teamJson?

The above is for the dart:io version (which, of course, you can use in Flutter) If you would like to stick with the package:http version, then you need to tweak your Map a bit. body must be a Map<String, String>. You need to decide what you want as your POST parameters. Do you want two: homeTeam and awayTeam? or one, say, teamJson?

此代码

Map<String, String> body = { 'name': 'doodle', 'color': 'blue', 'homeTeam': json.encode( {'team': 'Team A'}, ), 'awayTeam': json.encode( {'team': 'Team B'}, ), }; Response r = await post( url, body: body, );

在电线上产生这个

name=doodle&color=blue&homeTeam=%7B%22team%22%3A%22Team+A%22%7D&awayTeam=%7B%22team%22%3A%22Team+B%22%7D

name=doodle&color=blue&homeTeam=%7B%22team%22%3A%22Team+A%22%7D&awayTeam=%7B%22team%22%3A%22Team+B%22%7D

或者,这个

Map<String, String> body = { 'name': 'doodle', 'color': 'blue', 'teamJson': json.encode({ 'homeTeam': {'team': 'Team A'}, 'awayTeam': {'team': 'Team B'}, }), }; Response r = await post( url, body: body, );

在电线上产生这个

name=doodle&color=blue&teamJson=%7B%22homeTeam%22%3A%7B%22team%22%3A%22Team+A%22%7D%2C%22awayTeam%22%3A%7B%22team%22%3A%22团队+B%22%7D%7D

name=doodle&color=blue&teamJson=%7B%22homeTeam%22%3A%7B%22team%22%3A%22Team+A%22%7D%2C%22awayTeam%22%3A%7B%22team%22%3A%22Team+B%22%7D%7D

package:http 客户端负责:对 Uri.encodeQueryComponent 进行编码,utf8 编码(注意这是默认的,所以不需要指定它)并在 Content-Length 中发送长度标题.您仍然必须进行json编码.

the package:http client takes care of: encoding the Uri.encodeQueryComponent, utf8 encoding (note that that's the default, so no need to specify it) and sending the length in the Content-Length header. You must still do the json encoding.

更多推荐

如何在flutter中使用url编码的正文发出HTTP POST请求?

本文发布于:2023-10-09 18:42:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476472.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   正文   flutter   url   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!