Can't save a file containing semicolon (;)

I’m using this code for saving a file. And it doesn’t work when the text contains semicolon. Is there a special option for semicolon?

create_file(Request) :-
	member(method(post), Request), !,
	http_parameters(Request,
                [
                  text(Text)
                ],
                [attribute_declarations(param)]
        ),
	open('apps/output.txt', write, Stream),
	write(Stream, Text),
	close(Stream).

write/2 definitely doesn’t care whether the thing to write has semicolons or not. Possibly the param hook declares the text parameter such that it cannot be read properly?

What is the error? Normal debugging is to use ?- tspy(create_file). and re-run the request. That should popup the debugger.

Param hook:

param(text, [optional(true)]).

There is only one error ERR_EMPTY_RESPONSE, because I don’t response anything. Debugger shows nothing. Perhaps I am wrongly sending the ajax request? I’m using CodeMirror as an editor.

$.ajax({
	  type: 'POST',
	  url: '/user/file',
	  data: 'text=' + myCodeMirror.getValue(),
	  success: function(data) {
	    console.log(data);
	  }
	});

This way you get an illegal www-form-encoded content. You either need to encode and package this correctly or simply use data: myCodeMirror.getValue(). That needs some adjustment to the Prolog side as well, where hyou now need:

handler(Request) :-
    option(method(post), Request), !,
    http_read_data(Request, String, [to(string)]),
    ....

That might be because there is no valid request, so you don’t get this far.

Your solution works fine. And how can I reply status 200, if I only want to close the stream and that is all?

The streams are managed outside the handlers. You do need to reply some content though. You can do so using reply_json_dict/1,2, reply_html_page/2,3 or as simple as

format('Content-type: text/plain~n~n'),
format('Saved!~n').

For API style requests I’d normally use a JSON response.

1 Like