我正在使用 http 依赖项发出 http post 请求。我在回复中遇到以下错误。我在下面发布我的代码:
flutter: Error on line 1, column 32: Invalid media type: expected /[^()<>@,;:"\\\/[\]?={} \t\x00-\x1F\x7F]+/.
╷
1 │ application/json;charset=utf-8;
│
^
下面是我遇到错误的代码:
try {
String url = 'https://app.restroapp.com/';
Map<String, String> headers = {"Content-type": "application/json"};
String json = '{"device_id": "abaf785580c22722", "user_id": "", "device_token": "","platform":"android"}';
// make POST request
Response response = await post(Uri.encodeFull(url), headers: headers, body: json);
// check the status code for the result
int statusCode = response.statusCode;
// this API passes back the id of the new item added to the body
String body = response.body;
print(statusCode);
print(body);
} catch (e) {
print(e);
}
最佳答案
将其用于发布请求
Future<Map<String, dynamic>> postRequest(String url, Map jsonMap) async{
print('$url , $jsonMap');
HttpClient httpClient = new HttpClient();
HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));
request.headers.set('content-type', 'application/json');
request.add(utf8.encode(json.encode(jsonMap)));
HttpClientResponse response = await request.close();
String reply = await response.transform(utf8.decoder).join();
print(reply);
httpClient.close();
Map<String, dynamic>map = json.decode(reply);
return map;
}
关于Flutter:Http 发布请求错误无效的媒体类型:预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369129/