How to get post requests to work for an Azure Python Web App?
Elliot Duncan
0
Reputation points
I've been working on a web app and have been able to get it to send back an image when running it locally, but I can't get it to work once I have deployed it to Azure. Whenever I try to run a post request, I get the following error, that shows up on the azure web app's logs as well as the error handling for the code sending the request. How would I go about getting it so that I could send a post request to this web app and get a response?
The error is:
Failed to retrieve the image. Status code: 405
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>
The deployed code is as follows:
import
import
import
import
from
from
import
import
def
logging.info(
# Check if an image was uploaded
if
return
json.dumps({
status_code
mimetype
)
image
if
return
json.dumps({
status_code
mimetype
)
# Save the uploaded image temporarily
filename
file_path
with
f.write(image.read())
# Process the image using process_img function
tactile_image_path
and I am sending a request for the time being with the following code:
import requests
import mimetypes
import os
import time
def send_image_post_request(url, image_path):
# Open the image file in binary mode
with open(image_path, 'rb') as image_file:
# Send a POST request with the image as part of the form data
mime_type, _ = mimetypes.guess_type(image_path) # Get the mime type, causing issues RN
files = {'file': (image_path, image_file, mime_type)}
response = requests.post(url, files=files)
return response # Return the response object
def save_image_from_response(response, image_path):
if response.status_code == 200:
output_dir = 'tmp'
# Create a unique output filename, using the MIME type from the response or file extension
mime_type, _ = mimetypes.guess_type(image_path) # Get MIME type for the file
file_extension = mime_type.split('/')[1] # Get file extension (e.g., 'jpeg')
output_filename = f"tactile_{int(time.time())}.{file_extension}" # Unique filename with timestamp
# Ensure the directory exists
os.makedirs(output_dir, exist_ok=True)
# Define the full path to save the image
output_path = os.path.join(output_dir, output_filename)
# Save the image content to a file
with open(output_path, 'wb') as out_file:
out_file.write(response.content)
print(f"Image downloaded successfully and saved as {output_path}")
else:
print(f"Failed to retrieve the image. Status code: {response.status_code}")
print(response.text)
def main():
url = "https://tactileimagegenerator-a2d9g9gwg4dfdjfe.westus-01.azurewebsites.net/"#input("Enter the URL: ")
image_path = "images_requests/old_main_snow.jpeg"#input("Enter the image path: ")
# Send POST request and get the response
response = send_image_post_request(url, image_path)
# Save the image from the response
save_image_from_response(response, image_path)
# Run the main function
main()
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,932 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,966 questions
Sign in to answer