0

I have 2 endpoints:

  • enpoint A can only be called if endpoint B has been called before at least once. After endpoint B has been called, endpoint A can be called multiple times.
  • endpoint B can be called multiple times without problems.

Endpoint B loads certain data that must be present in the system, before we import additional data using A. The data is different between the two endpoints, but B is needed to enrich the one that A will import.

I am currently throwing an exception in the controller of endpoint A if endpoint B has not been called before.

I would like to know if there is a best practice in REST to signal or enforce this behavior.

In case there is not, is it ok if I return 400 bad request for it or there is a more specific status code?

5
  • 1
    Some context would be appropriate here. Is B a login method?
    – Flater
    Commented Dec 14, 2020 at 10:24
  • @Flater added some context Commented Dec 14, 2020 at 10:29
  • 3
    Rest doesn't have a concept of endpoints. Rest has resources.
    – bdsl
    Commented Dec 14, 2020 at 10:29
  • 1
    As others have pointed out, REST doesn't have endpoints. So what are A and B? Are they resources or methods on the same resource? If they are methods and A creates a new resource, it could be perfectly fine to return 404 if B is another method, but the resource doesn't exist.
    – Rik D
    Commented Dec 14, 2020 at 15:59
  • 1
    You might look into HATEOAS if you aren't already familiar with it: restcookbook.com/Basics/hateoas
    – Kyle McVay
    Commented Dec 16, 2020 at 0:02

3 Answers 3

10

Best practice is to avoid temporal coupling in REST. (Temporal coupling is requirements than operations should be performed in a specified order.)

Alternatively you could have a POST to A return the URL for B. Then there is no way to request B before A.

Your talk of "calling endpoint" indicates you think of this API more like a RPC service rather than a REST service. RPC is fine for certain purposes, but it is not REST. In REST we don't talk about calls and endpoints, we talk of resources and methods.

7

is it ok if I return 400 bad request

4xx does not look like a proper status code if the goal is only to prevent calling a method before another one. Specifically, 400 indicates that the server was unable to process the request sent by the client due to invalid syntax (or size too large, invalid request message framing, or deceptive request routing etc), which is not your case.

if there is a best practice in REST to signal or enforce this behavior

However a 404 Not Found may be fine if B relies on some resources created from A.

So the best practice is probably either that, or not allowing calling B rather than throwing the exception, like JacquesB suggested with an URL returned by a POST to A.

5

In my opinion, there's a design flaw if your system requires you to call a REST API in specific steps. As other user posted, you should avoid temporal coupling in REST. If A and B run processes, and calling B runs a process that sets the state of the system up before you can call A, that isn't REST!

REST Is largely about HTTP methods on resources. The state of the system isn't an expected consideration. Something exists, and you Get it, or something doesn't exist and you return 404 "Not Found". If B and A are resources, and A requires B, you need to reference an id for B when you Add/Put A into your system. That is how you require B to be run first.

If you insist on requiring a user call B before A, have B return a token or ID associated with the result. If endpoint A needs to accept the token/ID, you can guarantee that B has been run before A. If token/ID is not present (or not valid) in request to A, then it makes sense to return a 400 "Bad Request".

Not the answer you're looking for? Browse other questions tagged or ask your own question.