简体   繁体   English

在REST API中返回绝对vs相对URI

[英]returning absolute vs relative URIs in REST API

suppose the DogManagementPro program is an application written in client/server architecture, where the customers who buys it is supposed to run the server on his own PC, and access it either locally or remotely. 假设DogManagementPro程序是一个用客户端/服务器架构编写的应用程序,购买它的客户应该在自己的PC上运行服务器,并在本地或远程访问它。

suppose I want to support a "list all dogs" operations in the DogManagementPro REST API. 假设我想支持DogManagementPro REST API中的“列出所有狗”操作。

so a GET to http://localhost/DogManagerPro/api/dogs should fetch the following response now: 所以对http://localhost/DogManagerPro/api/dogs的GET应该现在获取以下响应:

<dogs>
  <dog>http://localhost/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://localhost/DogManagerPro/api/dogs/sparky</dog>
</dogs>

where I want to access it remotely on my local LAN, [the local IP of my machine is 192.168.0.33] what should aa GET to http://192.168.0.33:1234/DogManagerPro/api/dogs fetch? 我想在我的本地局域网远程访问它,[我的机器的本地IP是192.168.0.33]应该怎样获取http://192.168.0.33:1234/DogManagerPro/api/dogs fetch?

should it be: 它应该是:

<dogs>
  <dog>http://localhost/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://localhost/DogManagerPro/api/dogs/sparky</dog>
</dogs>

or perhaps: 也许:

<dogs>
  <dog>http://192.168.0.33/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://192.168.0.33/DogManagerPro/api/dogs/sparky</dog>
</dogs>

?

some people argue that I should subside the problem altogether by returning just a path element like so: 有些人认为我应该通过返回一个像这样的路径元素来完全解决问题:

<dogs>
  <dog>/DogManagerPro/api/dogs/ralf</dog>
  <dog>/DogManagerPro/api/dogs/sparky</dog>
</dogs>

what is the best way? 什么是最好的方法?

I've personally always used non-absolute urls. 我个人总是使用非绝对网址。 It solves a few other problems as well, such as reverse / caching proxies. 它还解决了一些其他问题,例如反向/缓存代理。

It's a bit more complicated for the client though, and if they want to store the document as-is, it may imply they also now need to store the base url, or expand the inner urls. 但是对于客户端来说有点复杂,如果他们想要按原样存储文档,则可能意味着他们现在还需要存储基本URL或扩展内部URL。

If you do choose to go for the full-url route, I would not recommend using HTTP_HOST, but setup multiple vhosts, and environment variable and use that. 如果您确实选择使用完整网址,我建议不要使用HTTP_HOST,而是设置多个虚拟主机和环境变量并使用它。 This solves the issue if you later on need proxies in front of your origin server. 如果您以后需要在源服务器前面的代理,这可以解决问题。

I would say absolute URLs created based on the Host header that the client sent 我会说基于客户端发送的Host头创建的绝对URL

<dogs>
  <dog>http://192.168.0.33:1234/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://192.168.0.33:1234/DogManagerPro/api/dogs/sparky</dog>
</dogs>

The returned URIs should be something the client is able to resolve. 返回的URI应该是客户端能够解析的内容。

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

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