如何使用python内置的request发送JSON格式的数据

使用步骤如下:

一、如果想发送json格式的数据,需要使用request模块中的Request类来创建对象,作为urlopen函数的参数

二、header中添加content-type为application/json

三、使用json中dumps方法将请求体内容解析为字符串类型

from urllib import request
import json

# 请求体数据
request_data ={
    "account": "xxxxxx",
    "sign": "xxxx"
    }

headers ={
    "content-type":"application/json"
}

req = request.Request(url = "http://host:port/mm.nn.zz",
                headers = headers,
                data = json.dumps(request_data).encode("utf-8"))
                
reps = request.urlopen(req).read().decode("utf-8")
原文地址:https://www.cnblogs.com/yaoqingzhuan/p/10057821.html