pythonjavaazureflaskokhttp

Python flask server 405 error for a specific path when hosted on Azure, works fine locally


I'm building a flask app to process images uploaded from a mobile device, before sending results back to the mobile app. I've successfully deployed the flask app on Azure, and can confirm it works with certain paths, /test for one.

The path my mobile app uses to upload an image, is /upload-data. When I run my flask app locally on my own PC and connect to it, this path works fine, accepting the image uploaded and returning a response.

However, when using the flask app deployed on Azure, the response I get for /upload-data is just 405 (Method not allowed).

This is the top part of my code in my python flask backend:

@app.route("/upload-data", methods=['POST'])
def store_data():
    #Image processing code etc...

And this is how I make the post to the above path in my java android app with OkHTTP:

private void uploadData(String imagePath, String pointCloudPath) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(responseTimeOut, TimeUnit.SECONDS)
                .build();
        File imageFile = new File(imagePath);

        //Create request body
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image", imageFile.getName(),
                        RequestBody.create(imageFile, MediaType.parse("image/jpeg")))
                .build();

        //Create request itself
        String url = "http://" + backendUrl;
        Request request = new Request.Builder()
                .url(url+"/upload-data")
                .post(requestBody)
                .build();

        //Execute request in background
        Log.d("OkHTTP Image Upload", "Creating image upload request to: " + request.url());
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
                Log.e("OkHTTP Image Upload", "Failed: " + e.getMessage());
                runOnUiThread(() -> textStatus.setText("Request Failed: " + e.getMessage()));
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                if (response.isSuccessful()) {
                    Log.d("OkHTTP Upload", "Success");
                    InputStream responseZipStream = response.body().byteStream();
                    runOnUiThread(() -> processAnalysisResponse(responseZipStream));
                }
                else {
                    Log.e("OkHTTP Image Upload", "Error: "+response.code());
                    runOnUiThread(() -> textStatus.setText("Request Error: "+response.code()));
                }
            }
        });
    }

Solution

  • I notice you're making your requests over HTTP not HTTPS, I imagine Azure is stricter on the POST requests rather than GET due to encrypting the body of the request.

    Make sure you have HTTPS only turned off in Azure Portal.