0

Usually to retrieve a resource one uses:

GET http://ws.mydomain.com/resource/123212

But what if your item IDs are HTTP URIs?:

GET http://ws.mydomain.com/resource/http://id.someotherdomain.com/SGX.3211

Browsers replace two slashes with one, and the request turns into:

GET http://ws.mydomain.com/resource/http:/id.someotherdomain.com/SGX.3211

which will not work.

URI encoding the "http://id.someotherdomain.com/SGX.3211" -part results in HTTP 400 - Bad request.

Is there a best practice for handling this?


Edit:

Then of course if we would need to have (I don't at the moment) request in form:

resources/ID/collections/ID

and all IDs are HTTP URIs, things get out of hand... Possibly one could do something like this and parse the contents inside the curly braces:

resources/{http://id...}/collections/{http://id...}

4

1 回答 1

0

Encode the other system's URI, and then pass the value as a query parameter:

GET http://ws.mydomain.com/resource?ref=http%3A%2F%2Fid.someotherdomain.com%2FSGX.3211

Ugly looking, but no one said that URIs used in a REST architecture have to be beautiful. :)

By the way, a GET actually looks like this when it's sent:

GET /resource?ref=http%3A%2F%2Fid.someotherdomain.com%2FSGX.3211 HTTP/1.1
Host: ws.mydomain.com

UPDATE: apparently you no longer have to encode "/" and "?" within a query component. From RFC 3986:

The characters slash ("/") and question mark ("?") may represent data within the query component. Beware that some older, erroneous implementations may not handle such data correctly when it is used as the base URI for relative references (Section 5.1), apparently
because they fail to distinguish query data from path data when looking for hierarchical separators. However, as query components are often used to carry identifying information in the form of "key=value" pairs and one frequently used value is a reference to another URI, it is sometimes better for usability to avoid percent-encoding those characters.

So you could legally do this:

GET /resource?ref=id.someotherdomain.com/SGX.3211 HTTP/1.1
Host: ws.mydomain.com
于 2013-11-14T20:52:28.447 回答