简体   繁体   English

REST如何在放入资源时处理查询参数?

[英]REST how to handle query parameters when put to resource?

I have a REST data service where I want to allow the users to create new items with HTTP PUT using different formats like json,xml,csv. 我有一个REST数据服务,我希望允许用户使用不同的格式(如json,xml,csv)使用HTTP PUT创建新项目。 I'm unsure how to best handle the format specification in the url: 我不确定如何在url中最好地处理格式规范:

PUT /ressource/ID/json
PUT /ressource/ID/xml

or 要么

PUT /ressource/ID?format=json
PUT /ressource/ID?format=xml

So what is the best way to specify a format indicator? 那么指定格式指标的最佳方法是什么?

If I specify the format with an query parameter and want to do a PUT how can I do this with curl? 如果我使用查询参数指定格式并想要进行PUT ,我该如何使用curl执行此操作?

curl -T test/data.json -d "format=json"  http://localhost:5000/resource/33

does not work. 不起作用。

curl -T test/data.json http://localhost:5000/update?format=json

works, but I would rather let curl build the query parameters instead of adding them by myself. 工作,但我宁愿让curl构建查询参数,而不是自己添加它们。

A general principle of RESTful web services is to use the features built-in to HTTP, when applicable. RESTful Web服务的一般原则是在适用时使用HTTP内置的功能。 In this case, you can indicate the format of your PUT request's content by setting the Content-Type header to application/json or application/xml . 在这种情况下,您可以通过将Content-Type标头设置为application / jsonapplication / xml来指示PUT请求内容的格式。

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

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