邮递员,Python以及将图像和元数据传递到Web服务 [英] Postman, Python and passing images and metadata to a web service
问题描述
这是一个由两部分组成的问题:我看到过讨论的各个部分,但是似乎无法获得推荐的建议来一起工作。我想创建一个Web服务来存储从调用者传递来的图像及其元数据,并从Postman进行测试调用以确保其正常工作。因此,要通过邮递员将图像(Drew16.jpg)传递到Web服务,看来我需要这样的东西:
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 store images and their metadata passed from a caller and run a test call from Postman to make sure it is working. So to pass an image (Drew16.jpg) to the web service via Postman, it appears I need something like this:
对于Web服务,我有一些python / flask代码可以读取请求(我尝试过的多种变体之一):
For the web service, I have some python/flask code to read the request (one of many variations I have tried):
from flask import Flask, jsonify, request, render_template
from flask_restful import Resource, Api, reqparse
...
def post(self, name):
request_data = request.get_json()
userId = request_data['UserId']
type = request_data['ImageType']
image = request.files['Image']
数据部分没有问题,并且JSON格式,但添加图像一直是个麻烦。我的邮递员配置哪里出问题了?从帖子中读取元数据和文件的实际Python命令集是什么? TIA
Had no problem with the data portion and straight JSON but adding the image has been a bugger. Where am I going wrong on my Postman config? What is the actual set of Python commands for reading the metadata and the file from the post? TIA
推荐答案
请原谅几乎是博客文章。我之所以发布此信息,是因为尽管您可以在各个地方找到部分答案,但我在任何地方都找不到完整的帖子,这可以为我节省大量时间。问题是您需要故事的双方来进行验证。
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.
所以我想使用Postman向Python / Flask Web服务发送请求。它必须具有图像以及一些元数据。
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.
以下是邮递员的设置(URL,标题):
Here are the settings for Postman (URL, Headers):
和正文:
现在进入Web服务。这是一个简单的服务,它将接受请求,打印元数据并保存文件:
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)
享受!
这篇关于邮递员,Python以及将图像和元数据传递到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!