Postman, Python and passing images and metadata to a web service

后端 未结 2 811
醉酒成梦
醉酒成梦 2020-12-18 08:28

this is a two-part question: I have seen individual pieces discussed, but can\'t seem to get the recommended suggestions to work together. I want to create a web service to

相关标签:
2条回答
  • 2020-12-18 08:55

    Make sure `request.files['Image'] contains the image you are sending and follow http://flask.pocoo.org/docs/1.0/patterns/fileuploads/ to save the file to your file system. Something like

    file = request.files['Image']
    file.save('./test_image.jpg')
    

    might do what you want, while you will have to work out the details of how the file should be named and where it should be placed.

    0 讨论(0)
  • 2020-12-18 09:03

    Pardon the almost blog post. I am posting this because while you can find partial answers in various places, I haven't run across a complete post anywhere, which would have saved me a ton of time. The problem is you need both sides to the story in order to verify either.

    So I want to send a request using Postman to a Python/Flask web service. It has to have an image along with some metadata.

    Here are the settings for Postman (URL, Headers):

    And Body:

    Now on to the web service. Here is a bare bones service which will take the request, print the metadata and save the file:

    from flask import Flask, request
    
    app = Flask(__name__)        
    
    # POST - just get the image and metadata
    @app.route('/RequestImageWithMetadata', methods=['POST'])
    def post():
        request_data = request.form['some_text']
        print(request_data)
        imagefile = request.files.get('imagefile', '')
        imagefile.save('D:/temp/test_image.jpg')
        return "OK", 200
    
    app.run(port=5000)
    

    Enjoy!

    0 讨论(0)
提交回复
热议问题