python使用requests发送application/json报文数据

时间:2023-03-08 23:20:44
python使用requests发送application/json报文数据
    def client_post_jsondata_requests(request_url,requestJSONdata):
#功能说明:发送json请求报文到指定的地址并获取请求响应报文
#输入参数说明:接收请求的URL,请求报文数据,格式为:{"param1":"123456","param2":"123456"}
#输出参数:请求响应报文
#by xiaocc[20180709]
     import requests requestJSONdata=str(requestJSONdata).replace("+", "%2B")
requestdata=requestJSONdata.encode("utf-8") head = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'} print '客户端请求JSON报文数据为(客户端 --> 服务端):\n',requestdata #客户端发送请求报文服务端
r = requests.post(request_url,data=requestdata,headers=head) #获取服务端的响应报文数据
responsedata=r.text
print '服务端的响应报文为(客户端 <--服务端): ',responsedata
print "get the status: ",r.status_code #返回请求响应报文
return responsedata