简体   繁体   English

我在哪里定义我想在 Django API 中接收的 Content-Type?

[英]Where do I define the Content-Type I want to receive in my Django API?

I have a Django app that only needs to receive requests with Content-Type: text/plain.我有一个 Django 应用程序,它只需要接收 Content-Type: text/plain 的请求。 Is there an option for that when using the GenericViewSet?使用 GenericViewSet 时是否有此选项?

I had to write my own Parser for plain text (in my case, the body contains json although the Content-Type is plain text, so I just had to override media_type) like this:我必须为纯文本编写自己的解析器(在我的例子中,正文包含 json,尽管 Content-Type 是纯文本,所以我只需要覆盖 media_type),如下所示:

class PlainTextParser(JSONParser):
    media_type = "text/plain"

and use it in my ViewSet like this:并像这样在我的 ViewSet 中使用它:

parser_classes = [PlainTextParser]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM