I’m using: SWI-Prolog version: 9.3.24 for x86_64-linux
I am writing a web server in Prolog and I want it to return an image file, but only if the user is authorized; otherwise I want to return an error response “Bad request”.
I have seen that some tutorials suggest to do “throw(http_reply(400))”, but I to don’t want to return an error page, because the application does not expect a page, but a binary file. I want it to return something like this:
you have to send the http header first of all.
the header contains the information for the response, and the body length
for the message you would like to send.
I think that is mostly the problem with the application. It is quite common that a request for a location may return a (text) error document rather than the expected type.
And no, you cannot affect the first line of the reply this way. That is always generated. The files http_exception.pl and http_header.pl provide hooks that first map exceptions to a Prolog term expressing the reply and then into a concrete document. That can be used to formulate arbitrary replies for error and non-200 status pages. I’d stay away from this though and fix the client.
% http_open/3 opens a HTTP connection
% request_header(Name = Value) set a HTTP header
% setup_call_cleanup/3 close the stream
% read_string/3 read server response as string
@paule32, thank you for looking into this, but it seems that either you don’t understand my question, or I don’t understand your answer.
My previous post was related to Jan’s comment: " The files http_exception.pl and http_header.pl provide hooks that first map exceptions to a Prolog term expressing the reply and then into a concrete document.That can be used to formulate arbitrary replies for error and non-200 status pages."
If possible, I would like Jan (or somebody else) to elaborate on this, and provide an example how would I use these hooks to generate a non-200 status reply.