Mandar Autade
Mandar Autade

Reputation: 356

Should a missing Path Parameter in a REST call result in 400 or 404?

If there are 2 usecase for REST Service,

  1. GET http://localhost:8080/resource/{id} ==> that returns all the data of the resource as per "id"

    So if client requests without "id" path parameter like GET http://localhost:8080/resource/ then what should be an appropriate response to this request. Should it be 400 or 404 ?

    Please note that there is no Service hosted for GET http://localhost:8080/resource/

  2. GET http://localhost:8080/users/{id}/history/ ==> that returns history details of user as per "id"

    So if client requests without "id" path parameter like GET http://localhost:8080/users/history/ then what should be an appropriate response to this request. Should it be 400 or 404 ?

    Please note that there is no Service hosted for GET http://localhost:8080/users/history/

Upvotes: 1

Views: 1606

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57279

So if client requests without "id" path parameter like GET http://localhost:8080/resource/ then what should be an appropriate response to this request. Should it be 400 or 404 ?

404.

  • That's the error code that calls the caller's attention to the effective request uri specifically
  • 404 is cacheable by default. You want clients and proxies to remember and reuse the 404 response. You don't get that behavior with 400 (which can cover a wide variety of problems).

Same argument holds for both of your examples.

Upvotes: 1

Related Questions