library(http/http_client) library provides more
powerful access to reading HTTP resources, providing keep-alive
chunked transfer and conversion of the content, such as
breaking down multipart data, parsing HTML, etc. The library
announces itself as providing
- http_get(+URL, -Reply, +Options)
- Performs a HTTP GET request on the given URL and then reads the reply
Defined options are:
close(default) a new connection is created for this request and closed after the request has completed. If
'Keep-Alive'the library checks for an open connection on the requested host and port and re-uses this connection. The connection is left open if the other party confirms the keep-alive and closed otherwise.
- Indicate the HTTP protocol version used for the connection. Default is
- proxy(+Host, +Port)
- Use an HTTP proxy to connect to the outside world.
- Send authorization to the proxy. Otherwise the same as the
- If provided, set a timeout on the stream using set_stream/2.
With this option if no new data arrives within Timeout
seconds the stream raises an exception. This option also affects data
being written by the server: if the client does not process the next
block of data (4096 bytes using the default setup) within Timeout,
the connection is terminated. Default is to wait forever (
- Defines the value of the
User-Agentfield of the HTTP header. Default is
- Ask for partial content. Range is a term
Unit(From, To), where From is an integer and To is either an integer or the atom
end. HTTP 1.1 only supports Unit =
bytes. E.g., to ask for bytes 1000-1999, use the option
- request_header(Name = Value)
- Add a line "Name: Value" to the HTTP request header. Both name and value are added uninspected and literally to the request header. This may be used to specify accept encodings, languages, etc. Please check the RFC2616 (HTTP) document for available fields and their meaning.
- Unify Header with a list of Name=Value pairs expressing all header fields of the reply. See http_read_request/2 for the result format.
Remaining options are passed to http_read_data/3.
- http_post(+URL, +In, -Reply, +Options)
- Performs a HTTP POST request on the given URL. It is equivalent to http_get/3, except for providing an input document, which is posted using http_post_data/3.
- http_read_data(+Header, -Data, +Options)
- Read data from an HTTP stream. Normally called from http_get/3
When dealing with HTTP POST in a server this predicate can be used to
retrieve the posted data. Header is the parsed header.
Options is a list of
Name(Value)pairs to guide the translation of the data. The following options are supported:
- Do not try to interpret the data according to the MIME-type, but return
it literally according to Target, which is one of:
- Append the data to the given stream, which must be a Prolog stream open for writing. This can be used to save the data in a (memory-)file, forward it to process using a pipe, etc.
- Return the result as an atom. Though SWI-Prolog has no limit on the size of atoms and provides atom-garbage collection, this options should be used with care.1Currently atom-garbage collection is activated after the creation of 10,000 atoms.
- Return the page as a list of character-codes. This is especially useful for parsing it using grammar rules.
- Overrule the
Content-Typeas provided by the HTTP reply header. Intended as a work-around for badly configured servers.
to(Target)option is provided the library tries the registered plug-in conversion filters. If none of these succeed it tries the built-in content-type handlers or returns the content as an atom. The builtin content filters are described below. The provided plug-ins are described in the following sections.
- This is the default encoding mechanism for POST requests issued by a web-browser. It is broken down to a list of Name = Value terms.
Finally, if all else fails the content is returned as an atom.
- http_post_data(+Data, +Stream, +ExtraHeader)
- Write an HTTP POST request to Stream using data from Data
and passing the additional extra headers from ExtraHeader.
Data is one of:
- Send an HTML token string as produced by the library
library(html_write)described in section section 3.18.
- Send an XML document created by passing XMLTerm to xml_write/3.
The MIME type is
- xml(+Type, +XMLTerm)
xml(XMLTerm), using the provided MIME type.
- Send the contents of File. The MIME type is derived from the filename extension using file_mime_type/2.
- file(+Type, +File)
- Send the contents of File using the provided MIME type, i.e. claiming
- Same as string(text/plain, Codes).
- codes(+Type, +Codes)
- Send string (list of character codes) using the indicated MIME-type.
- cgi_stream(+Stream, +Len)
- Read the input from Stream which, like CGI data starts with a partial HTTP header. The fields of this header are merged with the provided ExtraHeader fields. The first Len characters of Stream are used.
- Send data of the MIME type
application/x-www-form-urlencodedas produced by browsers issuing a POST request from an HTML form. ListOfParameter is a list of Name=Value or Name(Value) .
- Send data of the MIME type
multipart/form-dataas produced by browsers issuing a POST request from an HTML form using
multipart/form-data. This is a somewhat simplified MIME
multipart/mixedencoding used by browser forms including file input fields. ListOfData is the same as for the List alternative described below. Below is an example from the SWI-Prolog Sesame interface. Repository, etc. are atoms providing the value, while the last argument provides a value from a file.
..., http_post([ protocol(http), host(Host), port(Port), path(ActionPath) ], form_data([ repository = Repository, dataFormat = DataFormat, baseURI = BaseURI, verifyData = Verify, data = file(File) ]), _Reply, ), ...,
- If the argument is a plain list, it is sent using the MIME type
multipart/mixedand packed using mime_pack/3. See mime_pack/3 for details on the argument format.
This plug-in library
breaks multipart documents that are recognised by the
Mime-Version: 1.0 in the
header into a list of Name = Value pairs. This
library deals with data from web-forms using the
encoding as well as the FIPA
This plug-in library
provides a bridge between the SGML/XML/HTML parser provided by
and the http client library. After loading this hook the following
mime-types are automatically handled by the SGML parser.
- Handed to
library(sgml)using W3C HTML 4.0 DTD, suppressing and ignoring all HTML syntax errors. Options is passed to load_structure/3.
- Handed to
xmlns(XML + namespaces). Options is passed to load_structure/3. In particular,
dialect(xml)may be used to suppress namespace handling.
- Handled to
sgml. Options is passed to load_structure/3.