gpt4 book ai didi

python - 包括 Django FileUpload 的 Content-disposition header

转载 作者:太空狗 更新时间:2023-10-30 00:31:13 24 4
gpt4 key购买 nike

我定义了一个接受文件的 API 端点(例如使用 Django REST Framework)。在 Django 中,可以在检查响应时使用内容配置 header 。

https://docs.djangoproject.com/en/1.11/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

现在,如果我们想在测试端点时设置 header ,我该如何使用 REST-Framework 的 APITestCase 包含此 header ?

到目前为止我试过的是,但它似乎不接受标题。

class TestSaleViews(APITestCase):
def test_sale_detail_view(self):
f = create_named_temporary_file()
files = {'archive': f}
basename = os.path.basename(f.name)
headers = {
'content-disposition': 'attachment; filename={}'.format(basename),
}
response = self.client.post(url, files, format='multipart', **headers)

最佳答案

找到答案了!

Django 在其 FileUploadParser 中有一个固定的关键字用于此 header 。它是:HTTP_CONTENT_DISPOSITION

所以我需要更换它,瞧:成功了!

headers = {
'HTTP_CONTENT_DISPOSITION': 'attachment; filename={}'.format(basename),
}

https://github.com/encode/django-rest-framework/blob/master/rest_framework/parsers.py#L206

关于python - 包括 Django FileUpload 的 Content-disposition header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44244553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com