Flutter 颤振:Http post请求错误无效的媒体类型:应为

Flutter 颤振:Http post请求错误无效的媒体类型:应为,flutter,dart,dart-pub,dart-http,Flutter,Dart,Dart Pub,Dart Http,我正在使用http依赖项发出http post请求。我在回答中面临以下错误。我在下面发布我的代码: flutter: Error on line 1, column 32: Invalid media type: expected /[^()<>@,;:"\\\/[\]?={} \t\x00-\x1F\x7F]+/. ╷ 1 │ application/json;charset=utf-8; │

我正在使用http依赖项发出http post请求。我在回答中面临以下错误。我在下面发布我的代码:

flutter: Error on line 1, column 32: Invalid media type: expected /[^()<>@,;:"\\\/[\]?={} \t\x00-\x1F\x7F]+/.
      ╷
    1 │ application/json;charset=utf-8;
      │

                            ^
flatter:第1行第32列出错:无效的媒体类型:应为/[^()@;:“\\\/[\]?={}\t\x00-\x1F\x7F]+/。
╷
1.│ application/json;字符集=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);
  }
试试看{
字符串url='0https://app.restroapp.com/';
映射头={“内容类型”:“应用程序/json”};
字符串json='{“设备标识”:“abaf785580c22722”,“用户标识”:““设备标识”:““平台”:“android”}”;
//发帖
Response-Response=wait-post(Uri.encodeFull(url),headers:headers,body:json);
//检查结果的状态代码
int statusCode=response.statusCode;
//此API传回添加到主体的新项的id
字符串体=response.body;
打印(状态代码);
印刷品(正文);
}捕获(e){
印刷品(e);
}
这是用邮递员写的,请看下图:

将此用于post请求

 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;
}
Future postRequest(字符串url,映射jsonMap)异步{
打印('$url,$jsonMap');
HttpClient HttpClient=新HttpClient();
HttpClientRequest=wait-httpClient.postrl(Uri.parse(url));
request.headers.set('content-type','application/json');
add(utf8.encode(json.encode(jsonMap));
HttpClientResponse response=等待请求。关闭();
String reply=wait response.transform(utf8.decoder.join();
打印(答复);
httpClient.close();
Mapmap=json.decode(回复);
返回图;
}

有一个dart包,为http请求提供一些帮助程序类。它支持向post请求添加头

Github: 安装时请使用:

dependencies:
  basic_utils: ^1.5.1
用法

映射头={
“内容类型”:“应用程序/json”
};
映射查询参数={
“一些”:“参数”
};
字符串url=“”;
字符串payloadAsString=“{\“foo\”:\“bar\”}”;
地图主体;
试一试{
body=wait HttpUtils.postForJson(url,payloadAsString,
queryParameters:queryParameters,headers:headers);
}捕获(e){
//处理异常,例如,如果响应状态代码!=200-299
}
//对反应体做些什么
印刷品(正文);
其他信息:

这些都是HttpUtils类中的方法


Future服务器响应中的
内容类型
标题不正确(末尾的分号)