# CORS error when requesting prolog webservice

**URL:** <https://swi-prolog.discourse.group/t/cors-error-when-requesting-prolog-webservice/1714>\
**Category:** Help!\
**Created:** [January 7, 2020, 5:43pm UTC](https://swi-prolog.discourse.group/t/cors-error-when-requesting-prolog-webservice/1714 "2020-01-07T17:43:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![worriedhugo](https://avatars.discourse-cdn.com/v4/letter/w/f08c70/32.png) [@worriedhugo](https://swi-prolog.discourse.group/u/worriedhugo)\
**Post date:** [January 7, 2020, 5:43pm UTC](https://swi-prolog.discourse.group/t/cors-error-when-requesting-prolog-webservice/1714/1 "2020-01-07T17:43:40Z")

</div>

I’m using: SWI-Prolog version 8.0.3 for x86\_64-linux

I want the code to: To be able to receive requests from external applications.

But what I’m getting is: I get the “Access-Control-Allow-Origin not present on requested source” error when requesting my prolog webservice

My code looks like this:

```prolog
:- use_module(library(http/http_cors)).
:- use_module(library(http/http_parameters)).
:- use_module(library(http/http_client)).
:- use_module(library(http/http_header)).
:- use_module(library(http/html_write)).
:- use_module(library(uri)).

:- set_setting(http:cors, [*]).

someurl("https://some.url").

:- http_handler('/test', test_predicate, []).

test_predicate(Request):-
  ((
    option(method(options), Request),!,cors_enable(Request,[methods([get,post,delete])]),format('~n')
  );(
  populateData,
  %additional code%
  format('Access-Control-Allow-Origin: *~n'),
  format('Content-type: application/json~n'), !)).

populateData:-
  add_Users(_).

addUsers(_Request):-
  cors_enable,
  obtainData(Data),
%some code%.

obtainData(Object):-
  someurl(URL),
  http_get(URL,Object,[]).

```

When I request the data from Postman everything works as expected so the problem must be in the way I use cors\_enable. Basically, when the test\_predicate runs it must execute the populateData predicate. Should I pass the `Request` parameter that I get in test\_predicate to populateData? Is that the issue or am I missing a piece of code?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 8, 2020, 8:29pm UTC](https://swi-prolog.discourse.group/t/cors-error-when-requesting-prolog-webservice/1714/2 "2020-01-08T20:29:52Z")

</div>

This is pretty hard to read, but you seem to send the CORS header twice and do not finish the header with a double newline. I’d use something like below. Using two clauses is a lot more readable than ;/2 with !-s.

```prolog
test_predicate(Request) :-
    option(method(options), Request),
    !,
    cors_enable(Request,[methods([get,post,delete])]),
    format('Content-type: text/plain\r\n'),
    format('~n'). 
test_predicate(Request) :-
    cors_enable,
    reply_json(_{msg: "Hello world!"}).

```

---

<div class="post-metadata">

**Author:** ![worriedhugo](https://avatars.discourse-cdn.com/v4/letter/w/f08c70/32.png) [@worriedhugo](https://swi-prolog.discourse.group/u/worriedhugo)\
**Post date:** [January 16, 2020, 2:16pm UTC](https://swi-prolog.discourse.group/t/cors-error-when-requesting-prolog-webservice/1714/3 "2020-01-16T14:16:14Z")

</div>

So the issue was with the second request. I had to pass the `Request` variable to the `populateData` predicate and from there I had to pass it to `addUser`.

Like this:

```prolog
test_predicate(Request):-
  ((
    option(method(options), Request),!,cors_enable(Request,[methods([get,post,delete])]),format('~n')
  );(
  populateData(Request),
  %additional code%
  format('Access-Control-Allow-Origin: *~n'),
  format('Content-type: application/json~n'), !)).

populateData(Request):-
  add_Users(Request).

addUsers(Request):-
  obtainData(Data),
%some code%.

```

This fixed the problem. As always thank you for your answer!!
