So, I have an endpoint that works more or less like this:
from flask import Flask, request, jsonify
from flask_cors import CORS
import json
from werkzeug.utils import secure_filename
import os
from mylib import do_stuff
path = os.getcwd()
UPLOAD_FOLDER = os.path.join(path, 'data')
# #load flask
app = Flask(__name__)
CORS(app)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['JSON_AS_ASCII'] = False
print(UPLOAD_FOLDER,flush=True)
@app.route('/upload', methods=['POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
if file:
try:
# Receives a file and saves on the server
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
print("saving_here {}".format(file_path))
file.save(file_path)
# The result here is a dict of dicts of dicts
# It consists of a dictionary of DataFrames().to_dict()
result = do_stuff(file_path)
response = app.response_class(
response=json.dumps(result ),
status=200,
mimetype='application/json'
)
return response
except Exception as e:
print(e,flush=True)
return "error"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port= <PORT>)
The main issue here is that on the front-end sometime I receive an answer with the "message" key inside data and sometime I receive without it(Which is what I expect). The incorrect response:
"response": {
"data": {
"message": "{\"0\": {\"0\": {\"Item\": \"Desinfetante 5L Max Talco Azul\", \"Qtd\": 2, \"UM\": \"GL\", \"Qtd_UM\": \"5L\", \"Qtd_Ttl\": \"10L\"}, \"1\": {\"Item\": \"Caf\\u00e9 A V\\u00e1cuo Tradicional 500G\", \"Qtd\": 10, \"UM\": \"PC\", \"Qtd_UM\": \"500g\", \"Qtd_Ttl\": NaN}}}"
},
"headers": {
"content-type": [
"application/json"
],
"content-length": [
"227"
],
"access-control-allow-origin": [
"*"
],
"server": [
"Werkzeug/1.0.1 Python/3.8.6"
],
"date": [
"Fri, 11 Dec 2020 13:16:32 GMT"
]
},
"status": 200,
"statusText": "OK"
}
}
The expected response (only the data entry):
"response": {
"data": {
"0": {
"0": {
"Pedido": 997,
"Qtd": 5,
"Item": "Água Sanitária 1 Litro",
"Fornecedor": "YYYY"
},
"1": {
"Pedido": 997,
"Qtd": 2,
"Item": "Limpa Vidros Audax Facilita 500ml",
"Fornecedor": "XXXX"
}}}
When I make a post directly from python as in:
import requests
files = {'file': open('<path_to_file>','rb')}
r = requests.post(url="<url>/upload", files = files)
r.json()
Out[12]:
{'0': {'0': {'Item': 'Desinfetante 5L Max Talco Azul',
'Qtd': 2,
'UM': 'GL',
'Qtd_UM': '5L',
'Qtd_Ttl': '10L'},
'1': {'Item': 'Café A Vácuo Tradicional 500G',
'Qtd': 10,
'UM': 'PC',
'Qtd_UM': '500g',
'Qtd_Ttl': nan}}}
r.text
Out[16]: '{"0": {"0": {"Item": "Desinfetante 5L Max Talco Azul", "Qtd": 2, "UM": "GL", "Qtd_UM": "5L", "Qtd_Ttl": "10L"}, "1": {"Item": "Caf\\u00e9 A V\\u00e1cuo Tradicional 500G", "Qtd": 10, "UM": "PC", "Qtd_UM": "500g", "Qtd_Ttl": NaN}}}'
I get the expected json response everytime and cannot recreate the issue I have with react, even with the same files and headers.
Things tried:
- return json.dumps(result)
- return jsonify(resutl)
- return response
from Response from flask has extra key on Json received by React front
No comments:
Post a Comment