Discrimination between infrastructure and business logic when using HTTP status codes

We are trying to create a REST interface that allows users to check for a specific resource. Suppose we sell domain names: the user needs to determine if the domain is available.

HTTP GETin conjunction with the codes of responses 200and 404it seems at first glance reasonable.

The problem is that we distinguish between a query that is successfully served by our search service and a query that is executed when the other components behave exclusively. For instance:

  • 404and 200can be returned by intermediate proxies that actually block the request. This may be due to improper proxy configuration or even external infrastructure such as a Wifi coffee maker using poor forms-based authentication.

  • Customers may use broken URLs. This can happen through obsolescence or (again) by incorrect configuration. However, we could fight the first with help 301.

What is the current best practice for distinguishing between responses that were successfully executed against the client’s intentions for this request and responses received through exceptional behavior?

The problem is resolved by tunneling the responses through the response body, as we can guarantee that they are unique to our service. However, it seems not very RESTful!

+4
source share
2 answers

Just add your content to your HTTP responses to distinguish them from responses received by intermediaries. Any or all of them will work:

  • Error information in the response content that is recognized as the content of your application (for example, Application error: Domain name not found (404))
  • A Content-Typein the response, which indicates that the content of the response should be decoded as an application error (e.g., Content-Type: application/vnd.domain-finder.error+json)
  • A custom header in the response that indicates that this is an application error.

​​, API , - , .

+2

" , RESTful, ", .

, API, :

/api/v1/domains/<name>/

/api/v1/domain/exists.com/ 200 whois.

/api/v1/domain/doesnt.com/ 404 .

, , . (, JSON results), API .

/api/v1/domains/?search=maybe
/api/v1/domains/?lookup=maybe.com

RESTful, ( ) . 200, .

0

Source: https://habr.com/ru/post/1537866/


All Articles