Flutter:Http post 请求错误媒体类型无效:预期

Flutter: Http post request Error Invalid media type: expected

我正在使用 http 依赖项发出 http post 请求。我在回复中遇到以下错误。我正在 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);
  }

它在postman中运行,请看下图:

将此用于 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;
}

有一个 dart 包为 http 请求提供了一些帮助程序 classes。它支持将 header 添加到 post 请求。

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 的方法class。

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);

服务器响应中的Content-Type header不正确(末尾的分号)。

https://github.com/dart-lang/http/issues/180#issuecomment-415774937

尝试:

String responseBody = utf8.decoder.convert(response.bodyBytes);