resthttphttpsrfc

What should be returned from http request on directory route?


tldr: I work with API which may have route like: http|s://<ip_or_domain>/hardcoded/<may_be_dir_or_may_be_file>

In app logic if user target directory i should give him list of files in that directory.

Generally in such case, usually i saw servers returning HTML containing list of files, and when the Content-Type header contains something else like application/json it returns serialized output.

ie. Should i return 404 when there is no Content-Type header or just assume user wants HTML?

My goal here is to have API which goes possibly close to standards. So far googled a bit for the topic, didn't found conclusive answer.


Solution

  • Generally in such case, usually i saw servers returning HTML containing list of files, and when the Content-Type header contains something else like application/json it returns serialized output.

    You don't want the Content-Type header here, you want the Accept header.

    The currently registered reference for Accept is RFC 9110. Basic idea here is that the client can send a weighted list of "media-ranges" that is understands, and the server is expected to produce the highest weight response that it is capable of.