我正在使用http依赖项发出http发布请求。我在回应中遇到以下错误。我在下面发布我的代码:
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);
}
答案 0 :(得分:1)
将此用于发布请求
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;
}
答案 1 :(得分:1)
有一个dart程序包,它为http请求提供一些帮助程序类。它支持将标题添加到发布请求。
Github:https://github.com/Ephenodrom/Dart-Basic-Utils 安装:
dependencies:
basic_utils: ^1.5.1
用法
Map<String, String> headers = {
"content-type": "application/json"
};
Map<String, String> queryParameters = {
"Some": "Parameter"
};
String url = "";
String payloadAsString = "{\"foo\":\"bar\"}";
Map<String, dynamic> body;
try {
body = await HttpUtils.postForJson(url, payloadAsString,
queryParameters: queryParameters, headers: headers);
} catch (e) {
// Handle exception, for example if response status code != 200-299
}
// do something with the response body
print(body);
其他信息:
这些都是HttpUtils类中的所有方法。
Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);
答案 2 :(得分:0)
服务器响应中的Content-Type
标头不正确(末尾是分号)。
答案 3 :(得分:0)
试试:
String responseBody = utf8.decoder.convert(response.bodyBytes);