gpt4 book ai didi

django-rest-framework - Django Rest Framework API 客户端自定义 header

转载 作者:行者123 更新时间:2023-12-03 15:59:26 31 4
gpt4 key购买 nike

我正在尝试使用来自 drf-extensions 的批量更新。为了使其工作,有一个安全措施需要标题“X-BULK-OPERATION”:'true'。我可以使用 curl 或我的 angular 应用程序使应用程序工作,但在我的测试中,我尝试使用 rest_framework.test.APIClient发送 partial_update 请求,但每次我收到 400 响应时,在调试请求时,我都会收到

ipdb> response.data
{'detail': "Header 'X-BULK-OPERATION' should be provided for bulk operation."}

这是我尝试在测试中使用的请求
    response = self.client.patch(
'/api/v1/db_items/?active=True',
json.dumps(data),
content_type='application/json',
**{X-BULK-OPERATION: 'true'}
)

有没有办法在 APIClient 请求上设置 header ?

我什至尝试更改标题名称并将其设置在凭据中
self.client.credentials(HTTP_BULK_OPERATION='true')

但我每次都会遇到同样的错误

最佳答案

答案记录极少,但似乎 django 对传入的 header 进行了自己的解析。我通过将代码更改为

    response = self.client.patch(
'/api/v1/db_items/?active=True',
json.dumps(data),
content_type='application/json',
**{'HTTP_X_BULK_OPERATION':'true'}
)

关于django-rest-framework - Django Rest Framework API 客户端自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53073763/

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