700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 如何使用request.post(Python)直接发送数组类型的方式

如何使用request.post(Python)直接发送数组类型的方式

时间:2020-06-03 09:20:28

相关推荐

如何使用request.post(Python)直接发送数组类型的方式

我们先来看一下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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。