flutter - 如何在 Flutter 中发布 x-www-form-urlencoded
问题描述
我正在尝试向POST
API 发送请求以创建帐户。
该请求运行良好,它应该如下所示:
还有 9 个标题是自动生成的,所以我没有显示它们,但如果需要,我可以再显示一个屏幕。
我的请求如下所示:
import 'dart:convert' as convert ;
import 'package:my_project/requests/utils.dart';
import 'package:http/http.dart' as http;
Future<String> createUser(String firstName, String name, String mail,
String password, String confirmPassword, String birthDate,
String phone) async {
String url = BASE_URL + "createUser" ; // Don't worry about BASE_URL, the final url is correct
Map<String, dynamic> formMap = {
"name": name,
"surname": firstName,
"mail": mail,
"password": password,
"birth": birthDate,
"phone": phone,
"confirmPassword": confirmPassword
} ;
http.Response response = await http.post(
url,
body: convert.jsonEncode(formMap),
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
encoding: convert.Encoding.getByName("utf-8"),
);
print("RESPONSE ${response.statusCode} ; BODY = ${response.body}");
return (response.body) ;
}
这是我的打印结果:
I/flutter ( 6942): RESPONSE 307 ; BODY =
如您所见,我收到 307 错误,并且问题不是来自服务器,因为它与 Postman 一起使用。
我form-urlencoded POST
是否正确发送此请求?
我也试过:
http.Response response = await http.post(
url,
body: "name=$name&surname=$firstName&mail=$mail&password=$password&birth=$birthDate&phone=$phone&confirmPassword=$confirmPassword",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
encoding: convert.Encoding.getByName("utf-8"),
);
但结果相同。我也试过:
http.Response response = await http.post(
url,
body: formMap,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
encoding: convert.Encoding.getByName("utf-8"),
);
再次得到相同的结果。
我究竟做错了什么 ?
编辑 :
我尝试了 FoggyDay 的回答,这是我现在的要求:
final client = HttpClient() ;
final request = await client.postUrl(Uri.parse(url));
request.headers.set(HttpHeaders.contentTypeHeader, "application/x-www_form-urlencoded");
request.followRedirects = true ;
request.write(formMap);
final response = await request.close();
print("STATUS CODE = ${response.statusCode}");
但是我仍然有一个 307 错误。我创建了正确的请求吗?
编辑 2:
按照要求,我打印的位置如下:
final client = HttpClient() ;
final request = await client.postUrl(Uri.parse(url));
request.headers.set(HttpHeaders.contentTypeHeader, "application/x-www_form-urlencoded");
request.followRedirects = true ;
request.write(formMap);
final response = await request.close();
print("STATUS CODE = ${response.statusCode}");
print("Response headers = ${response.headers}");
我得到:
I/flutter ( 7671): STATUS CODE = 307
I/flutter ( 7671): Response headers = location: /app/createUser/
I/flutter ( 7671): date: Tue, 26 May 2020 09:00:29 GMT
I/flutter ( 7671): content-length: 0
I/flutter ( 7671): server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips
问题是我已经在调用 /app/createUser...('/app/' 在 BASE_URL 中)
解决方案
对于x-www-form-urlencoded
参数,只需使用这个:
Future<String> login(user, pass) async {
final response = await http.post(
Uri.parse('https:youraddress.com/api'),
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
encoding: Encoding.getByName('utf-8'),
body: {"title": "title"},
);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
}
}
推荐阅读
- groovy - 如何根据两个键对映射值求和?
- c# - 如何返回列的最大值(文本字段)?
- c# - 如何为其中一个是活动项目的项目集合建模?
- ant - 通过 EXEC 使用不同的 JDK 运行 ANT(APT/WSGEN 解决方法)
- sql - Postgresql:计算缺少一些月份的每月运行总数
- javascript - 我想在动态创建的 div 上获取元素的值
- python - python 脚本不以 etc/rc.local 开头
- sql - 当另一个表中的另一个记录满足日期/时间条件时,在 SQL 中重新编码一个值
- react-native - 如何将 redux saga yield 导入我的 detox + jest 测试文件。我需要在我的测试设置中访问存储在 redux 存储中的数据
- node.js - 使用 zeit/pkg 打包 Node-Red