端点:http ://127.0.0.1:8000 /users/current/ 用于更新当前登录的用户。需要更新这些字段:当我使用 POSTMAN 将数据作为 JSON 发送到端点时:
{
"first_name": "Amir",
"last_name": "",
"profile": {
"location": "Florida",
"profession": "Not being useless",
}
}
Run Code Online (Sandbox Code Playgroud)
我应该如何在flutter中将其以http包形式发送?目前我通过以下方式发送:
Future<void> updateCurrentUserInformation(Teacher newTeacher) async {
const String url = "http://10.0.2.2:8000/users/current/";
await http.patch(
url,
body: {
"first_name": newTeacher.first_name,
"profile": "" //HOW DO I SHOULD SEND A MAP TO UPDATE LOCATION AND PROFESSION?
},
headers: {"Authorization": "JWT $authToken"},
).then((value) {
print(authToken);
print(value.body);
});
}
Run Code Online (Sandbox Code Playgroud)
我应该如何发送地图来更新位置和职业?
更新:即使我将配置文件编码为 JSON 服务器返回:{"profile":["This field is required."]}
这是代码:
Future<void> updateCurrentUserInformation(Athlete newTeacher) async {
const String url = "http://10.0.2.2:8000/users/current/";
var profile = json.encode({
"edu": newTeacher.education,
"location": newTeacher.location,
"profession": newTeacher.
"image": "",
});
await http.patch(
url,
body: {
"first_name": "amiramiramir",
"profile": profile
},
headers: {"Authorization": "JWT $authToken"},
).then((value) {
print(authToken);
print(value.body);
});
}
Run Code Online (Sandbox Code Playgroud)
服务器端收到的数据:
<QueryDict: {'first_name': ['amiramiramir'], 'profile': ['{"edu":"something","location":"somelocation","profession":"being useless"}']}>
Run Code Online (Sandbox Code Playgroud)
您可以将地图转换为 JSON 字符串。使用json.encode(yourMap)。
import 'dart:convert';
Future<void> updateCurrentUserInformation(Teacher newTeacher) async {
const String url = "http://10.0.2.2:8000/users/current/";
await http.patch(
url,
body: {
"first_name": newTeacher.first_name,
"profile": json.encode(newTeacher.yourMap) //HOW DO I SHOULD SEND A MAP TO UPDATE LOCATION AND PROFESSION?
},
headers: {"Authorization": "JWT $authToken"},
).then((value) {
print(authToken);
print(value.body);
});
}
Run Code Online (Sandbox Code Playgroud)