Django 对于REST请求,将协商哪种“格式”?
格式选择有三种变体:Django 对于REST请求,将协商哪种“格式”?,django,rest,django-rest-framework,Django,Rest,Django Rest Framework,格式选择有三种变体: curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.json" curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser?format=json" curl -uadmin:admin -H "Accept: text/html" "http://localhost:8080/alfresco/service/
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.json"
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser?format=json"
curl -uadmin:admin -H "Accept: text/html" "http://localhost:8080/alfresco/service/hellouser"
但从下一次查询的格式来看,这一点尚不清楚:
curl -uadmin:admin -H "Accept: text/html" "http://localhost:8080/alfresco/service/hellouser.xml?format=json"
我希望这里有json
。是否有人可以提供相关规范或文档的链接,这些规范或文档描述了{format}如何协商优先级?对于
Rails
,这样描述:
Rails从查询参数格式中获取所需的格式,如果没有,则从URL路径后缀获取,或者从Accept标头获取
UPD控制器可以处理所有提供的格式:
json
、xml
、html
UPD另一个极端情况:
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.pl?format=json"
curl -uadmin:admin "http://localhost:8080/alfresco/service/hellouser.pl?format=xml"
我相信你不会有200条回复,只有内容协商错误 代码显示:
?format=json
(format\u query\u param
)将由.xml
(format\u后缀
)XMLRenderer
左侧text/html
嘿,这是个聪明的问题@利诺维亚:好极了;-)我想应该是JSON,因为我不明白你的答案。
格式
谈判的优先顺序是什么?期待jsonjson
是正确的吗?也许我问得不对。我的回答是,你们将得到一个内容协商错误。XML将接管JSON,但无法遵守公认的text/html。稍后我将更新django rest文档,以明确这一点:)太好了。我已经投了赞成票。当您完成后,我将应用您的回答。第一段中描述的行为不包括:查询参数应优先于后缀。我在这个问题上又增加了一个角落案例。此外,我还提供了相关链接,其中描述了其他框架使用的优先级。