最近我将 Flask 应用程序部署到 Render,但遇到了在本地计算机上运行它时没有遇到的错误。
我收到的错误消息是:
D {message: 'Request failed with status code 502', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {…}, request: XMLHttpRequest, …}
我的应用程序由 Flask 后端组成,我正在使用 Axios 向下面的 Flask 路径发出请求。奇怪的是,该请求在本地运行时工作得很好。
这是我的烧瓶路线:
@app.route('/predict/tomato-leaf-disease-prediction', methods=['POST'])
def predict_tomato_leaf_disease():
image_file = request.files['image']
image = Image.open(image_file)
model_index = int(request.form.get('model'))
if model_index < 0: model_index = 0
model = tomato_leaf_models[model_index]
image = image.resize((112, 112))
image = np.expand_dims(image, axis=0)
predictions = model.predict(image)
return jsonify({
'Bacterial Spot': float(predictions[0][0]),
'Early Blight': float(predictions[0][1]),
'Late Blight': float(predictions[0][2]),
'Leaf Mold': float(predictions[0][3]),
'Septoria Leaf Spot': float(predictions[0][4]),
'Two-spotted Spider Mite': float(predictions[0][5]),
'Target Spot': float(predictions[0][6]),
'Yellow Leaf Curl Virus': float(predictions[0][7]),
'Mosaic Virus': float(predictions[0][8]),
'Healthy': float(predictions[0][9])
})
这是axios代码:
async predict(event) {
const url = window.location.origin + "/predict/tomato-leaf-disease-prediction "
this.loading = true
const formData = new FormData();
formData.append('image', this.imageFile);
this.model = + this.model
formData.append('model', this.model)
console.log(this.model)
axios.post(url, formData, {
headers: {
'Content-Type' : 'multipart/form-data',
accept: 'application/json'
}
})
.then(response => {
console.log(response.data);
this.predictions = response.data
this.first = false
this.loading = false
})
.catch(error => {
console.error(error);
});
},
我想知道 Render 上是否有任何特定配置或设置可能导致此错误,尤其是与 Flask 或 Axios 相关的配置或设置。我需要修改代码或渲染部署配置中的某些内容才能解决此问题吗?
任何建议或见解将不胜感激。谢谢!
我确保我在 Axios 请求中使用的 API 端点是正确且可访问的。我什至使用 cURL 或 Postman 等工具单独测试了端点,它返回了预期的响应。
我尝试尽可能优化我的代码
检查服务器的错误日志和部署设置。 502代码只发生在我的gunicorn服务器崩溃并且无法将页面重定向到nginx时。检查错误日志并重新启动服务器。我建议你使用supervisor+nginx组合进行部署。
希望我的回答对您有帮助!
我在 React 和 Node.js 中遇到了类似的问题,问题是通过 post 请求发送的数据未定义。我解决了在我的服务器或索引 .js 中导入“bodyParser”的问题,如下所示: `
import bodyParser from 'body-parser';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
`
在
app.use(express.json());
之前输入最后两个
希望我能帮忙。