How can I upload files in Postman?
Question:
I want to pass images through POST method in POSTMAN but I get None type response in the line request.files[‘image’]
I have tried a number of different things but I am not able to solve the problem.
from flask import Flask,jsonify
from flask import request
import face_recognition
import json
app = Flask(__name__)
@app.route('/')
def index():
return ''
@app.route('/valid_faces', methods=['POST'])
def POST():
if request.method == 'POST':
# getting the images url from the request
print('....')
name1 = request.files['file'] if request.files.get('file') else None
print('....')
print (name1) # name2 = request.form.get('name2')
# map the url to the ones in the folder images
firstImage = name1
# loading the image inside a variable
firstImage = face_recognition.load_image_file(firstImage)
result=face_recognition.face_locations(firstImage)
if result:
# x={'valid_faces':True}
# return jsonify(x)
return "True"
else:
# x={'valid_faces':False}
# return jsonify(x)
return "False"
if __name__ == "__main__":
app.run(debug=True)
Answers:
If you wish to append a file to a postman call it needs to be added to the body.
Check form-data
and select file
instead of text
. You can now select a file with the windows file explorer.
To obtain the given file use the request
package.
file = request.files['file']
The index has to be the same string you specified within the postman body data.
In my case Postman set empty first value for FileStorage object in request.files . You can check this using:
print(str(request.files))
And in case of empty index instead
file = request.files['file']
try to use
file = request.files['']
In Postman, do the following:
- Set the method to POST
- Set the body type to form-data
- Establish Key/Value pairs for input. Set the value for the Key to
"file"
- Mouse over the Key field and choose Text or File from the drop-down
menu
- Click Send to process the request
follow this process: https://i.stack.imgur.com/GGm4I.png
- Set Method to POST
- Change body tab to form-data
- Set key
- in Value, hover, you can see file/text. in here select file
- Select file you want to upload.
- Send request.
you can do it
files = request.files.getlist("file")
I want to pass images through POST method in POSTMAN but I get None type response in the line request.files[‘image’]
I have tried a number of different things but I am not able to solve the problem.
from flask import Flask,jsonify
from flask import request
import face_recognition
import json
app = Flask(__name__)
@app.route('/')
def index():
return ''
@app.route('/valid_faces', methods=['POST'])
def POST():
if request.method == 'POST':
# getting the images url from the request
print('....')
name1 = request.files['file'] if request.files.get('file') else None
print('....')
print (name1) # name2 = request.form.get('name2')
# map the url to the ones in the folder images
firstImage = name1
# loading the image inside a variable
firstImage = face_recognition.load_image_file(firstImage)
result=face_recognition.face_locations(firstImage)
if result:
# x={'valid_faces':True}
# return jsonify(x)
return "True"
else:
# x={'valid_faces':False}
# return jsonify(x)
return "False"
if __name__ == "__main__":
app.run(debug=True)
If you wish to append a file to a postman call it needs to be added to the body.
Check form-data
and select file
instead of text
. You can now select a file with the windows file explorer.
To obtain the given file use the request
package.
file = request.files['file']
The index has to be the same string you specified within the postman body data.
In my case Postman set empty first value for FileStorage object in request.files . You can check this using:
print(str(request.files))
And in case of empty index instead
file = request.files['file']
try to use
file = request.files['']
In Postman, do the following:
- Set the method to POST
- Set the body type to form-data
- Establish Key/Value pairs for input. Set the value for the Key to
"file" - Mouse over the Key field and choose Text or File from the drop-down
menu - Click Send to process the request
follow this process: https://i.stack.imgur.com/GGm4I.png
- Set Method to POST
- Change body tab to form-data
- Set key
- in Value, hover, you can see file/text. in here select file
- Select file you want to upload.
- Send request.
you can do it
files = request.files.getlist("file")