我正在使用Flutter和Django,当尝试调用后端
时出现此错误后台错误:{detail:不支持的媒体类型"text/plain;charset = utf-8"在请求。}
颤振代码:
Future<void> addAlbum(Album album) async {
print('5');
final url = Uri.parse('http://127.0.0.1:8000/list-create/');
try {
print('6');
final response = await http.post(url,
headers: {
'Authorization': 'Token dc7e96ed776bc44d013b44fdfe560a616c64646f',
},
body: json.encode({
'title': album.title,
'cover': album.cover, // => this is an int
'photos_number': album.photos_number, // => this is an int
}));
print('6 ed');
final responseData = json.decode(response.body);
print(responseData.toString());
final newAlbum = Album(
title: album.title,
cover: album.cover,
photos_number: album.photos_number,
);
print('6 edddgg');
_items.add(newAlbum);
print('6 f');
notifyListeners();
} catch (error) {
print('7');
print(error);
throw error;
}
}}
添加json内容类型头。
headers: {
'Authorization': 'Token dc7e96ed776bc44d013b44fdfe560a616c64646f',
'Content-Type': 'application/json; charset=UTF-8',
},