Issue
Using Requests, I need to send numpy arrays with json data in a single post to my flask application. How do I do this?
Solution
To convert a numpy array arr
to json, it can be serialized while preserving dimension with json.dumps(arr.tolist())
. Then on the api side, it can be parsed with np.array(json.loads(arr))
.
However, when using the requests json
parameter, the dumping and loading is handled for you. So arr.tolist()
is all that is required on the client, and np.array(arr)
on the api. Full example code below.
Client:
params = {'param0': 'param0', 'param1': 'param1'}
arr = np.random.rand(10, 10)
data = {'params': params, 'arr': arr.tolist()}
response = requests.post(url, json=data)
API:
@app.route('/test', methods=['POST'])
def test():
data = request.json
params = data['params']
arr = np.array(data['arr'])
print(params, arr.shape)
return "Success"
Output:
{'param0': 'param0', 'param1': 'param1'} (10, 10)
Note: When either the files
or data
parameter is being used in requests.post
, the json
parameter is disabled.
Answered By - Jordan Patterson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.