Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What status code to use when a parameter is missing in the query string?

Tags:

rest

http

rfc

I have an endpoint that requires a parameter passed via the query string (is a GET verb).

What is the appropriated status code to give when this parameter is missing from the request? 400 is the one? or should I respond with a 404?

[GET /search?q=ok] => 200 OK
[GET /search] => 400 Bad Request? or 404 Not Found? Or 422 Unprocessable Entity? Others?
like image 503
Diego Oliveira Avatar asked Aug 22 '18 15:08

Diego Oliveira


People also ask

What is missing parameter HTTP code?

400 errors indicate an invalid request, which means either that mandatory parameters are missing, or that syntactically invalid parameter values have been detected (for example an expected URL being text only). 404 errors indicate that a requested API service cannot be found, or that a requested entity cannot be found.

When should I use HTTP 400?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

What does the request is missing parameters mean?

Merchant Support. This error occurred because the feed file you tried to upload contains one or more rows with missing information. Each row in your feed must have the information filled out for all required attributes. If any row is missing this information, you will receive an error.

What is the status code for data not found?

404 Not Found: This response code occurs when the server cannot find the resources being requested by the client. This code can also be sent by the server instead of error 403 to hide the resources from the unauthorized client. It is one of the most famous response codes on the web.


1 Answers

TLDR It's an HTTP 400 - Bad Request.

It's a 400 because the user did not send the Required input field.

why not 422 - because this case fits to 400. Keeping your consumers in mind, you shouldn't go to non-popular response codes if you don't really need to.

Cases for HTTP 404:

1) Url which the client requested is not existing in your server (usually this will be handled by your server. Application developer usually doesn't have to do anything unless you want a nice looking 404 page and SEO reasons).

2) If it was a path parameter and client was looking for an entity with an id (for Example (/students/{id} and your application couldn't find such entity, you may respond with an HTTP 404.

Let's say, user send the query parameter and you did not find any items matching the query param, make no mistake, it's still an HTTP 200 with body as an empty array or so (not a 404 unlike mentioned in the previous case)

like image 190
so-random-dude Avatar answered Sep 28 '22 16:09

so-random-dude