-1

I searched "HTTP code for replacing a resource" on Google but it really messes up with the keywords and don't give me the desired results. I have a file upload API and an interface that users interact to manage their images, two situations can occur: the user can replace an existing image or he can send more images. When the user replaces an existing image what HTTP code should I return?

Its's not a duplicate because I'm asking what HTTP code should I return when replacing a file not what people think about returning null in a PUT operation. If a person answered the possible duplicate question with information that answer my question that is another story, the question itself is not the same.

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
  • Have a look at https://stackoverflow.com/questions/2342579/http-status-code-for-update-and-delete – Thiyagu Jan 06 '18 at 12:33
  • 1
    Possible duplicate of [Should a RESTful 'PUT' operation return something](https://stackoverflow.com/questions/797834/should-a-restful-put-operation-return-something) – Oliver Charlesworth Jan 06 '18 at 12:33
  • According to the possible duplicate I should use PUT for replacing and POST for creating, is that correct? I would use POST for both. – Diego Alves Jan 06 '18 at 12:46

2 Answers2

0

HTTP code is mainly for HTTP server's status, eg: server error, file not found on server, etc.

In your application, your file upload API should return a JSON response to tell the user the upload result. and the HTTP code can always be 200 for succeeded operations. For failed operations, choose a code you like.

JSON response example:

HTTP 200
{
'result': 'ok',
}


HTTP 200
{
'result': 'replaced',
}

HTTP 403/405/etc
{
'result': 'failed',
'reason': 'file exists'
}

Ref:

HTTP status code for update and delete?

http://www.restapitutorial.com/lessons/httpmethods.html

shawn
  • 4,305
  • 1
  • 17
  • 25
  • Always returning 200 sounds like the opposite of REST. – melpomene Jan 06 '18 at 12:34
  • Well, if someone do not like 200, just define and use 4xx for special cases. No official HTTP code for such small case like "replacing". – shawn Jan 06 '18 at 13:53
  • I'm objecting to returning 200 for errors. The 4xx range is for client errors, not server errors. – melpomene Jan 06 '18 at 13:54
  • well, I edited. My meaning is 200 for succeeded operations like "file replaced". For error codes, choose someone you like. – shawn Jan 06 '18 at 13:58
0

I have a file upload API and an interface that users interact to manage their images, two situations can occur: the user can replace an existing image or he can send more images. When the user replaces an existing image what HTTP code should I return?

Probably a 200 OK; the client made a request, you handled it successfully.

http://racksburg.com/choosing-an-http-status-code/

The actual implementation details of what the server actually did, or what the client intended, are not all that important; status codes are part of the meta data that allows generic http components to understand what's going on, so that they can behave appropriately (do we need to invalidate caches, should we pro-actively load some other resource, and so on).

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91