http - 如何在颤动的http post请求中发送自定义标头
问题描述
我的颤振项目中有http包。我想发送带有自定义标头的发布请求。这是我的代码片段。它将使用 http 作为自定义标头明确我的问题。它总是运行 else 语句意味着响应类型不是 200,它给了我提供的错误令牌无效。但在邮递员身上,它工作正常
Map data = {
'user_fullname': _name,
'user_address': _address,
'user_mobile': _phone,
};
var tokenData = {
'User_token': token,
'Content-Type': 'application/x-www-form-urlencoded'
};
final response = await http.post(url, body: data, headers: tokenData);
if (response.statusCode == 200) {
print(response.body);
} else {
print(response.body);
}
解决方案
我猜你正在将无效类型传递给发布请求。(标题必须是Map<String, String>
(我不确定飞镖tokenData
在运行时从什么推断),正文可以是动态的,等等)
final String url = 'YOUR_API_URL';
final Map<String, String> tokenData = {
"Content-type": "application/x-www-form-urlencoded",
"user_token": token
};
final Map<String, String> data = { //is _phone a String?
'user_fullname': _name,
'user_address': _address,
'user_mobile': _phone,
};
final Response response = await post(url, headers: tokenData, body: data);
if (response.statusCode == 200) {
print(response.body);
} else {
print(response.body);
}
}
正文只能是 a String
、 aList<int>
或 aMap<String, String>
如https://pub.dev/documentation/http/latest/http/Client/post.html所述
推荐阅读
- mongodb - mongodb聚合,过滤对象的内部数组
- botframework - 是否可以在他们的 REST API 之上为 LinkedIn 创建一个聊天机器人应用程序?
- asp.net - 如何将 ASP.NET MVC 5 Angular 4 应用程序部署到 IIS 服务器中?
- python - Python 3:N 是奇数且大于 - 代码无法正常工作
- outlook - 在 Outlook 中添加新的敏感度字段
- python - 基于mkl的numpy多线程动态控制
- apache-spark - 在 pyspark.ml 中使用 RandomForestClassifier 时,maxCategories 在 VectorIndexer 中未按预期工作
- scala - 是否可以编写一个不可变的双向链表?
- neo4j - 使用 neo4j-admin 导入时,我可以在不同的 id 空间中指定不同的 id 类型吗
- hexagon-dsp - 无法运行六边形 dsp 的 gaussian7x7 示例