我们先来看一下request的源码
"""Constructs a :class:`Request <Request>`, prepares it and sends it.Returns :class:`Response <Response>` object.:param method: method for the new :class:`Request` object.:param url: URL for the new :class:`Request` object.:param params: (optional) Dictionary or bytes to be sent in the querystring for the :class:`Request`.:param data: (optional) Dictionary, list of tuples, bytes, or file-likeobject to send in the body of the :class:`Request`.:param json: (optional) json to send in the body of the:class:`Request`.:param headers: (optional) Dictionary of HTTP Headers to send with the:class:`Request`.
可看到入参数有data、json,若还有其他的参数使用可变参数字典形式进行传递了kwargs
data类型传参有字典、列表元祖等类型,json传参就是将参数转化为json格式进行传递的。
传递numpy时,我们一般将数组转为bytes然后传递给data参数。
我们这里举例:
发送数据为
keypoints = result['preds']dic={}dic['index']=keypoints.tolist()dicJson = json.dumps(dic)headers = {'Content-Type': 'application/json'}url = 'http://10.11.24.23:8384/getimg'# response = requests.post(url= url, headers=headers, data=img_encoded.tobytes())response = requests.post(url= url, headers=headers, json=dicJson)
获取数据
pose_results = request.json #获取到jsonpose_results = json.loads(pose_results)#list数据#np.array(pose_results['index']).shape转为numpy