2

In web-API client made a get request with a parameter, to process something and get data from the file system. The parameter requested through the API a folder path. If API found that path, it is processing something on the resources inside and returning the processed content with Http-200 status code. This is perfect.

What if the folder exists and while processing some content missing in the path to process. Should API return Http-200 with empty content or Http-204 with/without response message?

jidh
  • 172
  • 1
  • 6
  • 22

1 Answers1

1

It depends on how you view the situation and how you interpret the result:

  • 200 - The request successfully executed
  • 204 - The request executed successfully but didn't produce anything to return
  • 400 - The request was not successful as the resource doesn't exist

you could use any of the above if you feel it is correct to do so, if a user tries to find an account via a GetAccount function that accepts an account number as input and that account doesn't exist I would return a 400 bad request error as the account did not exist, but I would also return an error string with something like 'unrecognised account number'

phuzi
  • 12,078
  • 3
  • 26
  • 50
Mike
  • 171
  • 1
  • 7
  • 2
    "400 - The request was not successful as the resource doesn't exist" did you mean 404. 400 - Bad Request normally means there was something wrong with the request itself. – phuzi Jul 05 '19 at 07:55
  • its a difficult one, 404 would suggest the endpoint didn't exist but if the endpoint accepted the request and processed it and it didn't have a return because the user asked for something outside the scope then I'd return a 400. I think its a murky area, I personally feel 404 should be reserved for situations the requested URL doesn't exist but I think personal choice comes in a lot here and as long as its documented in your API doc then you have a fair bit of artistic licence – Mike Jul 05 '19 at 08:01
  • 2
    it's not a bad request 400, since the api receive valid parameters and it's even processing with that, – jidh Jul 05 '19 at 08:02
  • 1
    @mike, actually 400 and 404 are out of context here, since i have procee the request successful with input patams, but the result of the process is null / nothing. so i confused to choose 200 with empty array or 204 directly. – jidh Jul 05 '19 at 08:05
  • @Mike Check out this [question/answer](https://stackoverflow.com/questions/25378624/should-a-restful-api-return-400-or-404-when-passed-an-invalid-id), it reflects the consensus on use of 400 vs 404. – phuzi Jul 05 '19 at 08:08