charlieMadrid
charlieMadrid

Reputation: 21

What HTTP status response code return if the request is missing or wrong value(multiple choice) path parameter?

A REST API with path parameter: /search/{option} , the {option} parameter has a list of possible values "A","B","C". GET method is only used.

What is the appropriated status code to give when {option} parameter is passed with worng value( for example "D")? and if the parameter is missing?

If the path parameter is missing the final endpoint: /search/ This service doesn't exist.

Upvotes: 2

Views: 482

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57259

What is the appropriated status code to give when {option} parameter is passed with worng value( for example "D")? and if the parameter is missing?

404

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource

The target resource is the resource identified by the request line as the request-target. So in a request that begins...

GET /search/D HTTP/1.1

The target resource is that resource corresponding to the identifier /search/D.

Since /search/D is not a resource in your application, it doesn't have a current representation, and therefore 404 is the correct response -- it's the way that we announce to the client (and intermediate components) that there was a spelling error in the request-target.

The fact that your implementation routes requests to /search/D to the same function that handles /search/A, /search/B, and /search/C is an accident of your implementation, and need not be considered when designing your response.

The same argument holds for

GET /search/ HTTP/1.1
GET /search HTTP/1.1

You might, as part of the body of the response, send a message that describes some of the obvious alternative spellings :

HTTP/1.1 404 Not Found

Did you mean?

/search/A
/search/B
/search/C

404 responses are cacheable by default - generic clients can store the returned representation for later use, without needing to interpret the payload.

Upvotes: 2

Related Questions