Calling http rest API and parsing json response with (scryer) #Prolog

2024-03-02

In this simple code I’ll show how you can call a rest api (qliksense saas rest api) and parse its json response


?- use_module(library(dcgs)).
?- use_module(library(pairs)).
?- use_module(library(serialization/json)).
?- use_module(library(http/http_open)).

?- set_prolog_flag(answer_write_options, [max_depth(1000)]).

?- http_open("https://mytenant.eu.qlikcloud.com/api/v1/tenants/me", S, [request_headers(['Authorization'("Bearer MYKEY")])]), 
	get_n_chars(S, _N, HTML),
	phrase(json_chars(pairs(J)), HTML), 
	member(string("id")-string(ID), J).

 HTML = "{\"id\":\"myid\",\"hostnames\":[\"mytenant.eu.qlikcloud.com\"]}", J = [string("id")-string("myid"),string("hostnames")-list([string("mytenant.eu.qlikcloud.com)])], ID = "myid"

Enter your instance's address


More posts like this

Smart investment Problem with Prolog

2024-07-14 | #programming #prolog

Below my #prolog solution for Smart investment problem. It is a sample of Linear Programming using Prolog. A client of an investment firm has $10000 available for investment. He has instructed that his money be invested in particular stocks, so that no more than $5000 is invested in any one stock but at least $1000 be invested in each stock.

Continue reading 