postman 如何在flutter http中发送原始数据

gab6jxml  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(178)

我想在flutter http中发送原始数据,但数据不像JSON
下面是我在Postman

中的实现方法
并在Flutter中使用http进行了尝试,

Response res = await post(
      Uri.parse(baseUrl + endPoint),
      headers: {'Client-ID': clientId, 'Authorization': 'Bearer $accessToken'},
      body: jsonEncode('fields *'),
    );

在控制台上看到这个,

Error: XMLHttpRequest error.
pprl5pva

pprl5pva1#

将其添加为

var headers = {
  'Accept': 'application/json',
  'Content-Type': 'text/plain',

};
var request = http.Request('POST', Uri.parse('Your url'));
request.body = '''fields *''';
request.headers.addAll(headers);

http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
  print(await response.stream.bytesToString());
}
else {
  print(response.reasonPhrase);
}

或者,您可以很容易地看到它在Postman的代码请求中实现,只需选择代码图标并选择http-Dart

相关问题