HTTP Client
The http-client library is a high-level layer over libcurl for HTTP client requests that was developed during Google Summer of Code 2023. In comparison to the raw libcurl bindings, it offers a more user-friendly API to make HTTP requests in Fortran 2008 and newer. All common HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD) are supported. The library handles response headers, form data, file uploads, and basic authentication as well.
Installation
The client library depends on libcurl with additional development headers installed. On FreeBSD, run:
# pkg install ftp/curl
On Debian Linux, instead:
# apt-get install libcurl4 libcurl4-openssl-dev
The preferred method of linking the library is by using the
Fortran Package Manager, and adding the
dependency to the build manifest fpm.toml
of the project:
[dependencies]
http = { git = "https://github.com/fortran-lang/http-client.git" }
stdlib = "*"
Example
The following basic example program simply starts a GET request to a given URL, retrieves the response, and prints the response details to standard output:
! app/main.f90
program main
use :: http, only : response_type, request
implicit none (type, external)
type(response_type) :: response
! Send a GET request to retrieve plain-text file.
response = request(url='https://www.netlib.org/sparse/readme')
if (.not. response%ok) then
print '("Error message: ", a)', response%err_msg
stop
end if
! Print the response.
print '("Response Code: ", a)', response%status_code
print '("Response Length: ", a)', response%content_length
print '("Response Method: ", a)', response%method
print '("Response Content:", /, a)', response%content
end program main
A minimal build manifest fpm.toml
of the program may look
like:
# fpm.toml
name = "get"
[dependencies]
http = { git = "https://github.com/fortran-lang/http-client.git" }
stdlib = "*"
[[executable]]
name = "get"
link = [ "curl" ]
The project name (get
) can be chosen freely. The workspace
directory of the example project must equal this specific structure:
<project>/
app/
main.f90
fpm.toml
In the last step, execute the Fortran Package Manager to build and run the program:
$ fpm build --profile release
$ fpm run
All dependencies will be downloaded and compiled automatically. The binary
get
is written to directory ./build/<compiler>/app/
.
Fortran Libraries
- http-client: Official GitHub repository
References
- R. Dongre: Official API documentation
< cURL | [Index] | CGI > |