Scryer Prolog

Module http_open

:- use_module(library(http/http_open)).

Make HTTP requests.

This library contains the predicate http_open/3 which allows you to perform HTTP(S) calls. Useful for making API calls, or parsing websites. It uses Hyper underneath.

http_open(+Address, -Stream, +Options).

Yields Stream to read the body of an HTTP reply from Address. Address is a list of characters, and includes the method. Both HTTP and HTTPS are supported.

Options supported:

  • method(+Method): Sets the HTTP method of the call. Method can be get (default), head, delete, post, put or patch.

  • data(+Data): Data to be sent in the request. Useful for POST, PUT and PATCH operations.

  • size(-Size): Unifies with the value of the Content-Length header

  • request_headers(+RequestHeaders): Headers to be used in the request

  • headers(-ListHeaders): Unifies with a list with all headers returned in the response

  • status_code(-Code): Unifies with the status code of the request (200, 201, 404, ...)

Example:


?- http_open("https://www.example.com", S, []), get_n_chars(S, N, HTML).
   S = '$stream'(0x7fb548001be8), N = 1256, HTML = "<!doctype html>\n<ht ...".