I’m using: SWI-Prolog version version 9.2.1 for x64-win64
I want to make an api but I’m having problems with cors
It works fine when I use postman but from an FE created with react I have problems with the CORS
This is my code:
:-consult(main).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- use_module(library(http/http_path)).
:- use_module(library(http/http_parameters)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_cors)).
:- set_setting(http:cors, [*]).
% URL handlers.
:- http_handler('/solve', handle_solve, [method(post)]).
handle_solve(Request) :-
cors_enable,
http_read_json_dict(Request, DictIn),
Initial = DictIn.initial,
Goal = DictIn.goal,
transform_lists(Initial, L),
transform_lists(Goal, G),
solve(L, G, Response),
count(Response, N),
DictOut = _{moves: Response, quantity:N},
reply_json_dict(DictOut)
.
server(Port) :-
http_server(http_dispatch, [port(Port)]).
:- initialization
(current_prolog_flag(argv, [SPort | _]) -> true ; SPort='8000'),
atom_number(SPort, Port),
server(Port).