<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_ym19_1_1466148154006_18591">Ciao a tutti,</div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr">premetto che ho poca esperienza con il linguaggio, sto "tentando" di realizzare alcuni web services utilizzando python 2.7, Flask, sqlalchemy e Oracle.</div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr">In sostanza devo semplicemente esporre delle informazioni lette dal database.</div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr">Ho fatto un modulino di test, avviato il ws, ed effettuato la chiamata da browser. Fin qui tutto bene, finchè non ho trovato delle lettere accentate.</div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr">La chiamata è andata in errore ritornando il seguente messaggio:</div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr"><br></div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr"><span style="font-family: "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif; font-size: 14px; white-space: pre-wrap;" id="yui_3_16_0_ym19_1_1466148154006_19071">UnicodeDecodeError: 'utf8' codec can't decode byte 0xf9 in position 8: invalid start byte</span><br></div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr"><span style="font-family: "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif; font-size: 14px; white-space: pre-wrap;"><br></span></div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr">Sotto il codice incriminato, l'errore avviene sul jsonify:</div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19184"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19321">from flask_sqlalchemy import SQLAlchemy</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19185"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19341">from flask import Flask, jsonify </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19186"><font size="2">import json</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19187"><font size="2"><br id="yui_3_16_0_ym19_1_1466148154006_19188"></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19189"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19342">db = SQLAlchemy()</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19190"><font size="2"><br id="yui_3_16_0_ym19_1_1466148154006_19191"></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19192"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19340">app = Flask(__name__)</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19193"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19337">app.config['SQLALCHEMY_DATABASE_URI'] = 'oracle://user:pwd@ip/sid'</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19194"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19338">app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19195"><font size="2"><br id="yui_3_16_0_ym19_1_1466148154006_19196"></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19197"><font size="2">db.init_app(app)</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19198"><font size="2"><br id="yui_3_16_0_ym19_1_1466148154006_19199"></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19200"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_20082">class Article(db.Model):</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19201"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19347"> __tablename__ = 'ARTICLES'</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19202"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19346"> id = db.Column(db.Integer, primary_key=True)</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19203"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19345"> code = db.Column(db.String(64))</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19204"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19344"> description = db.Column(db.String(64))</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19205"><font size="2"> </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19206"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19336">@app.route('/articles/', methods=['GET'])</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19207"><font size="2">def articles(): </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19208"><font size="2"> </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19209"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19335"> sql = """select id, code, description from articles where code = '11203'"""</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19210"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19343"> results = Article.query.from_statement(sql)</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19211"><font size="2"> </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19212"><font size="2"> json_results = []</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19213"><font size="2"> </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19214"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_20081"> for result in results:</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19215"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_20079"> d = {'id': result.id,</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19216"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19372"> 'code': result.code,</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19217"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19349"> 'description': result.description</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19218"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_20080"> }</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19219"><font size="2"><br id="yui_3_16_0_ym19_1_1466148154006_19220"></font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19221"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19330"> json_results.append(d)</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19222"><font size="2"> </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19223"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19329"> return jsonify(articles=json_results)</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19224"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19228"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19353">if __name__ == '__main__':</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19229"><font size="2"> </font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19230"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19369"> app.debug = True</font></div><div dir="ltr" id="yui_3_16_0_ym19_1_1466148154006_19231"><font size="2" id="yui_3_16_0_ym19_1_1466148154006_19348"> app.run(host="0.0.0.0",port=5027)</font></div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr"><br></div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr">Se aggiungo a result.description<span style="font-size: small;" id="yui_3_16_0_ym19_1_1466148154006_19380"> </span><font size="3" id="yui_3_16_0_ym19_1_1466148154006_19408">la decodifica result.description.decode("latin1"), la risposta è corretta:</font></div><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr"><span style="font-family: "Courier New"; white-space: pre-wrap;">{</span><br></div><pre style="word-wrap: break-word;" id="yui_3_16_0_ym19_1_1466148154006_19488"> "articles": [
{
"code": "11203",
"description": "Uovo Rag\u00f9 Carne",
"id": 192
}
]
}</pre><pre style="word-wrap: break-word;" id="yui_3_16_0_ym19_1_1466148154006_19488"><font face="HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif" id="yui_3_16_0_ym19_1_1466148154006_19599"><span style="white-space: normal;" id="yui_3_16_0_ym19_1_1466148154006_19598">Immagino che problemi con i charsets siano abbastanza frequenti, c'è modo per evitare di dover impostare per ogni singolo campo alfanumerico la decodifica corretta? </span></font></pre><pre style="word-wrap: break-word;" id="yui_3_16_0_ym19_1_1466148154006_19488"><span style="white-space: normal; font-family: HelveticaNeue, "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;" id="yui_3_16_0_ym19_1_1466148154006_19803">P.S.: Lo stesso codice nella 3.5 funziona perfettamente senza attributo decode (che tra l'altro nella 3 hanno rimosso).</span><br></pre><div id="yui_3_16_0_ym19_1_1466148154006_18591" dir="ltr"><br></div></div></body></html>