4

我正在使用 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);
  }

它在邮递员中工作,请参见下图: 在此处输入图像描述

4

4 回答 4

5

将此用于发布请求

 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;
}
于 2019-08-06T05:25:04.170 回答
3

有一个 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);
于 2019-08-06T08:59:31.397 回答
2

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

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

于 2020-12-03T19:21:59.477 回答
1

尝试:

String responseBody = utf8.decoder.convert(response.bodyBytes);
于 2021-07-16T12:47:16.187 回答