我在烧瓶中有一个项目来获取参数并且它可以工作,如果我通过邮递员打电话。但是如果我用 java 代码来做,它会返回错误 400。烧瓶:@app.route('/predict', methods=["POST"])def predict(): print("\nPredict......") print(request.form) print(request.form['textToPredict']) print("\nPredict......2") print(request.form.get("textToPredict")) #print("Text...."+request.form['textToPredict'].toString()) # new text to predict text_to_predict = [request.form['textToPredict'].lower()] #[request.form.get('textToPredict')] # ["asmatica desde infancia ex fumante 15 am dopc em uso de o2 ha 1 ano em uso de alenia 400 /12 e formoterol 12 2 x dia mvdiminuido sra brn f 2 t s/s cta rx pfp"] # este deve retornar CID Z00 #print("Text....:"+str(text_to_predict)) # count_vect gera as posições dos vetores de cada palavra do texto. new = count_vect.transform(text_to_predict) # carrega o modelo treinado loaded_model = cPickle.load(open(fname, 'rb')) # faz a predição do novo texto de entrada result = loaded_model.predict(new) print(result) # accuracy_score(y_test, result) json_dict = request.get_json() text = ''.join(result) textPredicted = text data = {'textPredicted': textPredicted} return jsonify(data), 200java的输出(打印烧瓶代码):Predict......ImmutableMultiDict([('{"textToPredict":"coriza"}', '')])127.0.0.1 - - [22/Oct/2018 16:07:34] "POST /predict HTTP/1.1" 400 -邮递员的输出(打印 FLASK 代码):Predict......ImmutableMultiDict([('textToPredict', 'coriza, dificuldade em respirar, febre, dor no corpo')])coriza, dificuldade em respirar, febre, dor no corpoPredict......2coriza, dificuldade em respirar, febre, dor no corpo['J06 ']127.0.0.1 - - [22/Oct/2018 16:20:00] "POST /predict HTTP/1.1" 200 -所以,看起来我的错误是在发送帖子时,它是由 java 代码组成的。我不明白错误的原因。在我看来,参数在到达烧瓶时是不正确的,根据它提供的输出。我需要一些帮助来解决这个问题。谢谢你。
1 回答
精慕HU
TA贡献1845条经验 获得超8个赞
您的 java 代码不正确,因为您试图将 json 对象字符串发送到表单 url 编码数据中。看起来像你的 Flask 控制器需要表单参数。你可以像这样改变你的java代码
String urlParam = "textToPredict="+predicaoVo;
byte[] postData = urlParam.getBytes( StandardCharsets.UTF_8 );
int dataLength = postData.length;
postConnection.setRequestProperty( "Content-Length", Integer.toString(dataLength));
try( DataOutputStream wr = new DataOutputStream(postConnection.getOutputStream())) {
wr.write(postData);
}
添加回答
举报
0/150
提交
取消