Flask和Werkzeug:使用自定义标题测试发布请求 [英] Flask and Werkzeug: Testing a post request with custom headers
问题描述
我目前正在使用来自 http://flask.pocoo.org/docs / testing / ,但是我想添加一个标题到一个post请求。
我的请求是:
self.app.post('/ v0 / scenes / test / foo',data = dict(image =(StringIO('fake image'),'image。 png')))
但是我想添加一个content-md5到请求。这是可能的吗?
我的调查: testing.py)扩展Werkzeug的客户,记录在这里: 正如您所看到的, 所以看起来不支持。但是我怎么能得到这样一个功能,虽然? I'm currently testing my app with suggestions from http://flask.pocoo.org/docs/testing/, but I would like to add a header to a post request. My request is currently: but I would like to add a content-md5 to the request. Is this possible? My investigations: Flask Client (in flask/testing.py) extends Werkzeug's Client, documented here:
http://werkzeug.pocoo.org/docs/test/ As you can see, So it looks like it's not supported. How might I get such a feature working, though?
这篇关于Flask和Werkzeug:使用自定义标题测试发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
http://werkzeug.pocoo.org / docs / test /
post
使用开
。但是打开
只有:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ as_tuple - 以(environ,result)形式返回元组
buffered - 将其设置为True以缓冲应用程序运行。这也会自动关闭你的应用程序。
follow_redirects - 如果客户端应该遵循HTTP重定向,则将其设置为True。
* args
和 ** kwargs
来作为 EnvironBuilder
参数。因此,您可以在您的第一个发布请求中添加标头
参数:
以self.app.test_client()作为客户端:
client.post('/ v0 / scenes / test / foo',
data = dict(image =(StringIO('fake image')' image.png')),
headers = {'content-md5':'some hash'});
self.app.post('/v0/scenes/test/foo', data=dict(image=(StringIO('fake image'), 'image.png')))
post
uses open
. But open
only has:Parameters:
as_tuple – Returns a tuple in the form (environ, result)
buffered – Set this to True to buffer the application run. This will automatically close the application for you as well.
follow_redirects – Set this to True if the Client should follow HTTP redirects.
open
also take *args
and **kwargs
which used as EnvironBuilder
arguments. So you can add just headers
argument to your first post request:with self.app.test_client() as client:
client.post('/v0/scenes/test/foo',
data=dict(image=(StringIO('fake image'), 'image.png')),
headers={'content-md5': 'some hash'});