简体   繁体   English

关于使用HTTP请求标头在网站(HTML)和api(JSON)之间切换的意见

[英]Opinions on using HTTP request headers to switch between website (HTML) and api (JSON)

We have an ecommerce website that displays groups of products by category using a URL format that maps almost exactly to the REST URL format we would like to use for our forthcoming API. 我们有一个电子商务网站,该网站使用URL格式按类别显示产品组,该URL格式几乎完全映射到我们要用于即将发布的API的REST URL格式。

eg example.com/products/latest or example.com/products/hats 例如example.com/products/latestexample.com/products/hats

Is it a valid pattern to use the same URL for visible (HTML) and invisible (JSON) results, and to use the Accept http request header to determine what should be returned. 对可见(HTML)和不可见(JSON)结果使用相同的URL,并使用Accept HTTP请求标头确定应返回的内容是一种有效的模式。

ie if you call example.com/products/latest with Accept: application/json you get just the product data, but if you use text/html you get the full HTML page (header, footer, site chrome etc.) 即,如果您使用Accept: application/json调用example.com/products/latest,则仅获得产品数据,但是如果使用text/html获得完整的HTML页面(页眉,页脚,网站镶边等)。

And if so, is this a good idea - will we run into problems if, for instance, the website needs to change, but the API needs to be stable? 如果是这样,这是一个好主意吗?例如,如果网站需要更改,但API需要保持稳定,我们是否会遇到问题?

UPDATE : some helpful resources - here is an article[1] by Peter Williams discussing the use of the HTTP Accept header to version APIs, and I have also referenced an SO question[2] that reveals some of the problems of using this approach. 更新 :一些有用的资源-这是Peter Williams的文章[1],讨论了将HTTP Accept标头用于版本API,并且我还引用了一个SO问题[2],该问题揭示了使用此方法的一些问题。 Probably better to use a custom HTTP header? 使用自定义HTTP标头可能更好?

[1] Making the case for using Accept: http://barelyenough.org/blog/2008/05/versioning-rest-web-services/ [1]使用“接受”的理由: http : //barelyenough.org/blog/2008/05/versioning-rest-web-services/
[2] Problems with jQuery (& IE): Cannot properly set the Accept HTTP header with jQuery [2] jQuery(&IE)问题: 无法使用jQuery正确设置Accept HTTP标头
[3] Making the case for using Accept: http://blog.steveklabnik.com/2011/07/03/nobody-understands-rest-or-http.html [3]使用“接受”的理由: http : //blog.steveklabnik.com/2011/07/03/nobody-understands-rest-or-http.html
[4] Sitting on the fence: http://www.informit.com/articles/article.aspx?p=1566460 [4]坐在篱笆上: http : //www.informit.com/articles/article.aspx? p= 1566460

Using http headers is generally becoming the accepted way of determining this. 使用http标头通常已成为确定此标头的公认方法。

In ASP.NET MVC for example there is an IsAjaxRequest method that checks for the X-Requested-With header and if it is equal to "XMLHttpRequest" it is deemed to be an ajax request. 例如,在ASP.NET MVC中,存在一个IsAjaxRequest方法,该方法检查X-Requested-With标头,如果它等于"XMLHttpRequest"则将其视为ajax请求。

Last time I tried to do that (and this was a few years ago) I found I could not override the Accept header of an XMLHttpRequest object in Opera. 上一次我尝试这样做(这是几年前),我发现我无法覆盖Opera中XMLHttpRequest对象的Accept标头。 If that isn't a worry for you, then go for it, that is how HTTP was designed to work. 如果您不担心,那就去吧,这就是HTTP设计的工作方式。

I recommend setting your HTML response to have a higher q value then your JSON response though, some browsers send Accept: */* . 我建议将HTML响应设置为具有比JSON响应更高的q值,但是某些浏览器会发送Accept: */*

我对此没有经验,但是Restful Web Services建议您通过URL(例如api.example.com/v1/products/hats)对API进行版本控制-我不确定是否适合将相同的URL用于网站和API。

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

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