0

Considering this url: http://example.com/users/1/post/2/likes.

How can I correctly define structure of my 404 response, showing which item is not found? user, post or likes?

The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
hpaknia
  • 2,769
  • 4
  • 34
  • 63
  • What does this have to do with PHP? Are you using a rewrite rule that converts that to a PHP URL? – Barmar May 20 '17 at 00:32

1 Answers1

0

You can send an HTTP status code using the header() function, by starting the header with the status code number, followed by the message to send to the user.

header("404 " + $message);

The code that processes the parameters can determine which item isn't found, and put that into $message.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Can you explain what do you mean by message? By the way I'm returning a json object as result, why shouldn't I put this info there? – hpaknia May 20 '17 at 02:00
  • `$message` would contain something like `user is missing`. – Barmar May 20 '17 at 14:49
  • You asked how to put the information about which column is missing in the 404 response. Why would you use a 404 response if you're sending JSON? Just put status information in the JSON: `{ "success": false, "error": "user is missing"}` – Barmar May 20 '17 at 14:51
  • `"error": "user is missing"` is a sentence. What if my api sends Italian translation of this error. How frontend can detect which item is missing? Should frontend parse every possible messages? – hpaknia May 20 '17 at 20:06
  • 1
    You could send a more structured JSON, like `{success: false, missing_fields: ["users", "post"]}` – Barmar May 20 '17 at 22:41
  • If this is the structure you suggest, please update the answer :) – hpaknia May 21 '17 at 20:27
  • I answered the question you asked, which was how to send a message in the 404 response. If you want to know how to put a message in a JSON response, you should post a new question. – Barmar May 22 '17 at 01:56