Http reply for simple API

I’m working on a http server and I can get it to run on a Linux Mint machine as a daemon. The routes appear to work and I even have a simple route that kills the process. The routes work but the way I am doing the reply to the client tells me the server is sending an empty response and I think I misunderstand the doc section on creating a http reply. I’m not sending a header, and I’m pretty sure that’s the problem but there’s no html involved and I was wondering how I would reply a simple string.

The examples in the docs always assume html is involved. What would a reply clause look like if I just want to return a string?

To reply a simple string just use the CGI-style API:

handle(Request) :-
    format('Content-type: text/plain~n~n'),
    format('Hello World!~n').

Using that you can reply to just about anything. There are high level commands for replying files, directory indexes, HTML, JSON and various status messages (404, etc.)

I have a swi prolog http server running too. I have something like this , it contains the format content type as well.

:- http_handler( root( Fn ), reply_html_with_replacements(Fn),      [prefix]).

reply_html_with_replacements( File , Request  ):-
  % periodically_data_to_files(),
  atom_concat( 'web/' , File , Fx0 ) ,
  osdir_file( Fx0, Fx ) , 
  exists_file( Fx ) , ! ,
  http_parameters( Request, [], [ form_data( Datastr ) ] ) ,

  format( "Content-type: text/html~n~n" ),
  httpstr_to_mem( Datastr ) ,
  ip_to_mem( Request , Ip_adres_ato ) ,   ... etc


Thanks! I didn’t have a Content-type line or the following blank line.

A post was split to a new topic: HTTP DDoS protection