Retrofit POST 不适用于 Flask API,但 python 请求有效?
我创建了一个 Flask API,它获取手写数字的 Base64 图像并预测该数字。
@app.route('/predict', methods = ['POST'])
def predict():
if request.method == 'POST':
b64String = request.form['file']
prediction = predictResult(b64String)
return jsonify({'pred' : prediction.item()})
此 Flask 应用程序已托管在 https://mnist-flask-pytorch-ashis.herokuapp.com/predict
上。当我使用 python requests 测试端点时,它工作得很好。我用来测试的代码是:
def toBase64(filename):
encodedString = ''
with open(filename, 'rb') as img:
encodedString = base64.b64encode(img.read())
return encodedString
res = requests.post('https://mnist-flask-pytorch-ashis.herokuapp.com/predict', data={'file': toBase64('seven.png')})
print(res.text)
现在我想构建一个android应用程序来服务这个API,我正在使用retrofit并得到503。
interface MnistService {
@POST("predict")
suspend fun getPrediction(@Body file: String): Response<MnistResponse>
}
来自heroku的一些日志如果它可能有帮助
2022-03-08T11:56:44.674100+00:00 app[web.1]: 10.1.26.234 - - [08/Mar/2022:11:56:44 +0000] "POST /predict HTTP/1.1" 200 11 "-" "python-requests/2.26.0"
2022-03-08T11:57:27.139081+00:00 app[web.1]: 10.1.81.249 - - [08/Mar/2022:11:57:27 +0000] "POST /predict HTTP/1.1" 400 192 "-" "okhttp/4.9.1"
2022-03-08T11:57:27.149358+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/predict" host=mnist-flask-pytorch-ashis.herokuapp.com request_id=586590ba-ea2b-48a4-99cc-9d3969aa7f6e fwd="115.124.42.47" dyno=web.1 connect=0ms service=11ms status=503 bytes=355 protocol=https
I have created a flask API that takes a base64 image of a handwritten digit and predicts the digit.
@app.route('/predict', methods = ['POST'])
def predict():
if request.method == 'POST':
b64String = request.form['file']
prediction = predictResult(b64String)
return jsonify({'pred' : prediction.item()})
This Flask app is already hosted at https://mnist-flask-pytorch-ashis.herokuapp.com/predict
. When I am testing the endpoint using python requests it works perfectly fine. The code I am using to test is:
def toBase64(filename):
encodedString = ''
with open(filename, 'rb') as img:
encodedString = base64.b64encode(img.read())
return encodedString
res = requests.post('https://mnist-flask-pytorch-ashis.herokuapp.com/predict', data={'file': toBase64('seven.png')})
print(res.text)
Now I want to build an android app to serve this API, I am using retrofit and getting 503.
interface MnistService {
@POST("predict")
suspend fun getPrediction(@Body file: String): Response<MnistResponse>
}
Some logs from heroku if it may help
2022-03-08T11:56:44.674100+00:00 app[web.1]: 10.1.26.234 - - [08/Mar/2022:11:56:44 +0000] "POST /predict HTTP/1.1" 200 11 "-" "python-requests/2.26.0"
2022-03-08T11:57:27.139081+00:00 app[web.1]: 10.1.81.249 - - [08/Mar/2022:11:57:27 +0000] "POST /predict HTTP/1.1" 400 192 "-" "okhttp/4.9.1"
2022-03-08T11:57:27.149358+00:00 heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/predict" host=mnist-flask-pytorch-ashis.herokuapp.com request_id=586590ba-ea2b-48a4-99cc-9d3969aa7f6e fwd="115.124.42.47" dyno=web.1 connect=0ms service=11ms status=503 bytes=355 protocol=https
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您的服务人员所说,也许您应该使用表格来请求。
As your service said, maybe you should use form to request.