在dart:io中使用json content-type的Http POST请求

 mobiledu2502895137 发布于 2022-12-04 13:44

如何使用控制台dart应用程序执行HTTP POST(使用dart:io或可能是package:http库.我这样做:

import 'package:http/http.dart' as http;
import 'dart:io';

  http.post(
    url,
    headers: {HttpHeaders.CONTENT_TYPE: "application/json"},
    body: {"foo": "bar"})
      .then((response) {
        print("Response status: ${response.statusCode}");
        print("Response body: ${response.body}");
      }).catchError((err) {
        print(err);
      });

但是得到以下错误:

Bad state: Cannot set the body fields of a Request with content-type "application/json".

小智.. 29

这是一个完整的例子.您必须使用json.encode(...)将请求的主体转换为JSON.

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';


var url = "https://someurl/here";
var body = json.encode({"foo": "bar"});

Map headers = {
  'Content-type' : 'application/json', 
  'Accept': 'application/json',
};

final response =
    http.post(url, body: body, headers: headers);
final responseJson = json.decode(response.body);
print(responseJson);

通常建议您使用a Future来满足您的要求,这样您就可以尝试类似的东西

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';

Future requestMethod() async {
    var url = "https://someurl/here";
    var body = json.encode({"foo": "bar"});

    Map headers = {
      'Content-type' : 'application/json', 
      'Accept': 'application/json',
    };

    final response =
        await http.post(url, body: body, headers: headers);
    final responseJson = json.decode(response.body);
    print(responseJson);
    return response;
}

语法的唯一区别是asyncawait关键字.

2 个回答
  • 来自http.dart: ///[body]setsthebodyoftherequest.Itcanbea[String],a[List<int>]or ///a[Map<String,String>].IfitsaString,itsencodedusing[encoding]and ///usedasthebodyoftherequest.Thecontent-typeoftherequestwill ///defaulttotext/plain. 所以自己生成JSON主体(使用JSON.encodefromdart:convert).
    2022-12-11 01:55 回答
  • 这是一个完整的例子.您必须使用json.encode(...)将请求的主体转换为JSON.

    import 'package:http/http.dart' as http;
    import 'dart:convert';
    import 'dart:io';
    
    
    var url = "https://someurl/here";
    var body = json.encode({"foo": "bar"});
    
    Map headers = {
      'Content-type' : 'application/json', 
      'Accept': 'application/json',
    };
    
    final response =
        http.post(url, body: body, headers: headers);
    final responseJson = json.decode(response.body);
    print(responseJson);
    

    通常建议您使用a Future来满足您的要求,这样您就可以尝试类似的东西

    import 'package:http/http.dart' as http;
    import 'dart:convert';
    import 'dart:io';
    
    Future<http.Response> requestMethod() async {
        var url = "https://someurl/here";
        var body = json.encode({"foo": "bar"});
    
        Map<String,String> headers = {
          'Content-type' : 'application/json', 
          'Accept': 'application/json',
        };
    
        final response =
            await http.post(url, body: body, headers: headers);
        final responseJson = json.decode(response.body);
        print(responseJson);
        return response;
    }
    

    语法的唯一区别是asyncawait关键字.

    2022-12-11 02:57 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有