refer to code:
curl cmd.
.
curl -X POST -H "Content-Type: multipart/form-data" -H "Content-Type: application/json" -F 'data=@data.json;type=application/json' -H "Content-Type: image/jpeg" -F 'image=@image.jpg;type=image/jpeg' http://your-api-url.com/your-endpoint
..
flask code
.
from flask import Flask, request
import json
app = Flask(__name__)
@app.route('/your-endpoint', methods=['POST'])
def handle_upload():
if 'data' not in request.files:
return 'No data file uploaded', 400
if 'image' not in request.files:
return 'No image file uploaded', 400
data_file = request.files['data']
image_file = request.files['image']
if data_file.content_type != 'application/json':
return 'Invalid data file format', 400
if not is_valid_image_type(image_file.content_type):
return 'Invalid image file format', 400
# read the JSON data from the file
data = json.load(data_file)
# read the image data from the file
image_data = image_file.read()
# do something with the file data
return 'File upload successful'
def is_valid_image_type(content_type):
valid_image_types = ['image/jpeg', 'image/png', 'image/gif']
return content_type in valid_image_types
..
In this updated cURL command, the content type headers for the request and each file are specified using multiple -H
flags. The Content-Type
header for the request is set to multipart/form-data
, which is the required encoding for file uploads. The content type for the JSON file is set to application/json
, and the content type for the image file is set to image/jpeg
. Note that you should adjust the content types as needed for your specific file formats.
With these changes, you should be able to upload a JSON file and an image file to your Flask API using cURL. The uploaded files will be checked for errors in the API code and then processed as needed.
Than you.
🙇🏻♂️
www.marearts.com
No comments:
Post a Comment