'Retrofit POST not working for Flask API but python requests working?
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
Solution 1:[1]
As your service said, maybe you should use form to request.
interface MnistService {
@FormUrlEncoded
@POST("predict")
suspend fun getPrediction(@Field file: String): Response<MnistResponse>
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alex.Jia |
