# Why does including the library http\_json change the behavior of my existing code?

**URL:** <https://swi-prolog.discourse.group/t/why-does-including-the-library-http-json-change-the-behavior-of-my-existing-code/233>\
**Category:** Help!\
**Created:** [February 10, 2019, 3:28am UTC](https://swi-prolog.discourse.group/t/why-does-including-the-library-http-json-change-the-behavior-of-my-existing-code/233 "2019-02-10T03:28:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![careyphillips92](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@careyphillips92](https://swi-prolog.discourse.group/u/careyphillips92)\
**Post date:** [February 10, 2019, 3:28am UTC](https://swi-prolog.discourse.group/t/why-does-including-the-library-http-json-change-the-behavior-of-my-existing-code/233/1 "2019-02-10T03:28:01Z")

</div>

I’m using: SWI-Prolog version 8.0.1

This is a follow-up to an earlier question. I’m trying to update my old clunky PL server which was receiving and sending raw strings (and parsing them on the client side) to use JSON instead. The code below works. But if I uncomment the use\_module(library(http/http\_json)) directive, and make no changes to the main body of the code, it will fail, with:

% [Thread httpd@5000\_4] POST /dataexchange: [500] Stream (0x7fe688018280):1:8 Syntax error: json(illegal\_json)

My code looks like this:

```auto
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
% :- use_module(library(http/http_json)).
:- use_module(library(http/html_write)).

:- use_module(library(http/http_client)).
:- http_handler('/', web_form, []).

server(Port) :-
        http_server(http_dispatch, [port(Port)]).

web_form(_Request) :-
	reply_html_page(
	    title('POST demo'),
	    [
	       form([action='/', method='POST'], 
                [h1('Prolog Server')]
               )
        ]
    ).

:- http_handler('/dataexchange', dataexchange_handler, [time_limit(infinite)]).

dataexchange_handler(Request) :-
	debug_auto( ">>>RECEIVED request..."),
    convert_input_to_term( Request, Term ),
    debug_auto( "+++PROCESSING request"),
    general_predicate( Term, OutTerm ),
    debug_auto( "<<<REPLY"),
    reply_as_string( OutTerm ).

convert_input_to_term( Request, Term ) :-
    member(method(post), Request), !,
    http_read_data(Request, DataString, []),
    term_string( Term, DataString ).

general_predicate( InTerm, OutTerm ) :-
    call_findall_free_vars( InTerm, OutTerm ).
    
reply_as_string( Term ) :-
    term_string( Term, OutString ),
    format('Content-type: text/html~n~n', []),
    portray_clause(OutString).

```

Why does the mere inclusion of the http\_jason module cause failure? Does it have side-effects such as effectively hiding other functions?

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [February 10, 2019, 4:53am UTC](https://swi-prolog.discourse.group/t/why-does-including-the-library-http-json-change-the-behavior-of-my-existing-code/233/2 "2019-02-10T04:53:26Z")

</div>

This is a “plugin module” (see the docs). Yes, just including it changes the behaviour. You shouldn’t need to convert between strings and terms at all, you read json requests and write json responses. It is documented and explained in the docs: [http://www.swi-prolog.org/pldoc/man?section=httpjson](http://www.swi-prolog.org/pldoc/man?section=httpjson)

(I did not read your code to figure out what exactly causes the error. Can you provide a test case?)

---

<div class="post-metadata">

**Author:** ![careyphillips92](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@careyphillips92](https://swi-prolog.discourse.group/u/careyphillips92)\
**Post date:** [February 10, 2019, 5:34am UTC](https://swi-prolog.discourse.group/t/why-does-including-the-library-http-json-change-the-behavior-of-my-existing-code/233/3 "2019-02-10T05:34:00Z")

</div>

Yes, I had wanted to have an intermediate developmental phase of testing where I was sending strings to the server (the old system) but returning JSON (the new system) rather than having to change everything at once, and then altering the send phase subsequently.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [February 10, 2019, 5:48am UTC](https://swi-prolog.discourse.group/t/why-does-including-the-library-http-json-change-the-behavior-of-my-existing-code/233/4 "2019-02-10T05:48:16Z")

</div>

I guess during the transition you might want to use `library(http/json)` instead? Not sure, I used them both at some point but I have forgotten exactly what use case each covered. I am sure other would be able to help you with more specific advice.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [February 10, 2019, 5:49am UTC](https://swi-prolog.discourse.group/t/why-does-including-the-library-http-json-change-the-behavior-of-my-existing-code/233/5 "2019-02-10T05:49:17Z")

</div>

Maybe this will help:  
[http://www.swi-prolog.org/pldoc/doc\_for?object=atom\_json\_dict/3](http://www.swi-prolog.org/pldoc/doc_for?object=atom_json_dict/3)
