Python REST API 访问自定义 header 值

Python REST API access custom header values

我正在使用 django rest-framework 从客户端应用程序发布或获取数据。客户端应用程序随请求一起发送自定义 header。我如何才能在 api.

中访问自定义 header 值

Custome-Header-Key: ASDQWERYHH #This is the custom header set from the client.

请帮忙

谢谢

您可以使用 request.META 字典。

With the exception of CONTENT_LENGTH and CONTENT_TYPE, as given above, any HTTP headers in the request are converted to META keys by converting all characters to uppercase, replacing any hyphens with underscores and adding an HTTP_ prefix to the name. So, for example, a header called X-Bender would be mapped to the META key HTTP_X_BENDER.

因此,要在您的示例中获得 header 值,您可以这样做:

request.META['HTTP_CUSTOM_HEADER_KEY']