原创:python的requests.post()向后端传递数据,数组结构需将python数据结果转换成JSON

 针对采集来的数据,用requests.post()向后端传递

如果是python数据结构如数组,需要转换成为JSON对象,否则后端容易解析不成后端集合的对象结构 re

一:python做为前端请求requests.post()

     '''
        后台接收是数组
        '''
        dataInfoList = [
            {
                'area': 'string222',
                'city': 'string',
                'subject': 'request传递过去数据222'
            },
            {
                'area': 'string222',
                'city': 'string',
                'subject': 'request传递过去数据333'
            }
        ]
        # 请求头,不设置会理解成表单form post无法传递数据
        headers = {"content-type": "application/json"}
        #json.dumps()作用 将一个Python数据结构转换为JSON
        return22 = requests.post("http://192.168.0.100/api/v1/infor/listTdInfo", json.dumps(dataInfoList),
                                 headers=headers)
        print("传递是对象****requests.post提交返回结果:", return22)

 二:spring boot 做为后端接收前端传递过来数据

  /**
    * 接收scrapy 传递过来的字典里加集合
    * */
    @PostMapping("/listTdInfo")
    @ApiOperation(value = "为测试scrapy向后端提交数据222")
    public void addPostListTdInfo(@RequestBody List<TdInfor> tdInfor){
        System.out.println("添加数据是对象的集合如下");
        System.out.println(tdInfor);
    }

  

posted @ 2021-12-12 18:04  码哥之旅  阅读(1979)  评论(0编辑  收藏  举报