HTTP

Types

EasyCurl.HTTPResponseType
HTTPResponse <: CurlResponse

An HTTP response object returned on a request completion.

Fields

NameDescriptionAccessors (get)
status::IntThe HTTP status code of the response.http_status(x)
version::IntThe HTTP version received in the response.http_version(x)
total_time::Float64Total time spent receiving a response in seconds.http_total_time(x), curl_total_time(x)
body::Vector{UInt8}The response body as a vector of bytes.http_body(x), curl_body(x)
headers::Vector{Pair{String,String}}Headers received in the HTTP response.http_headers(x)
source
EasyCurl.HTTPOptionsType
HTTPOptions <: CurlOptions

Represents options for configuring an HTTP request.

Fields

NameDescriptionDefault
location::BoolAllow HTTP redirects.true
max_redirs::IntMaximum number of redirects.10
connect_timeout::RealMaximum time in seconds that you allow the connection phase to take.10
read_timeout::RealTimeout in seconds for reading the response.30
ssl_verifyhost::BoolEnables SSL certificate's host verification.true
ssl_verifypeer::BoolEnables the SSL certificate verification.true
verbose::BoolEnables detailed output from Curl (useful for debugging).false
username::Union{String,Nothing}Username for authentication.nothing
password::Union{String,Nothing}Password for authentication.nothing
proxy::Union{String,Nothing}Proxy server URL, or nothing to bypass proxy settings.nothing
interface::Union{String,Nothing}Specifies a particular network interface to use for the request, or nothing to use the default.nothing
accept_encoding::Union{String,Nothing}Specifies the accepted encodings for the response, such as "gzip".nothing
version::Union{UInt,Nothing}Specifies the CURL version to use, or nothing to use the default version available.nothing
source

Methods

EasyCurl.http_requestFunction
http_request(method::String, url::String; kw...) -> HTTPResponse

Send a url HTTP request using as method one of "GET", "POST", etc. and return a HTTPResponse object.

Keyword arguments

NameDescriptionDefault
queryRequest query dictionary or string.nothing
bodyRequest body.UInt8[]
headers::Vector{Pair{String,String}}Request headers.Pair{String,String}[]
status_exception::BoolWhether to throw an exception if the response status code indicates an error.true
retry::Int64Number of retries.0
retry_delay::RealDelay after failed request.0.25
options...Another HTTP request options.

Examples

julia> response = http_request(
           "POST",
           "http://httpbin.org/post";
           headers = Pair{String,String}[
               "Content-Type" => "application/json",
               "User-Agent" => "EasyCurl.jl",
           ],
           query = "qry=你好嗎",
           body = "{"data":"hi"}",
           interface = "en0",
           read_timeout = 5,
           connect_timeout = 10,
           retry = 10,
       );

julia> http_body(response) |> String |> print
{
  "args": {
    "qry": "你好嗎"
  },
  "data": "{"data":"hi"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Length": "13",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "EasyCurl.jl",
    "X-Amzn-Trace-Id": "Root=1-66e0d404-7597b1921ad026293b805690"
  },
  "json": {
    "data": "hi"
  },
  "origin": "100.250.50.140",
  "url": "http://httpbin.org/post?qry=你好嗎"
}
source
EasyCurl.http_getFunction
http_get(url::AbstractString; kw...) -> HTTPResponse

Shortcut for http_request function, work similar to http_request("GET", url; kw...).

Examples

julia> response = http_get(
           "http://httpbin.org/get";
           headers = Pair{String,String}[
               "Content-Type" => "application/json",
               "User-Agent" => "EasyCurl.jl",
           ],
           query = Dict("qry" => "你好嗎"),
       );

julia> http_body(response) |> String |> print
{
  "args": {
    "qry": "你好嗎"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "EasyCurl.jl",
    "X-Amzn-Trace-Id": "Root=1-66e0e18f-0e26d7757885d0ec1966ef64"
  },
  "origin": "100.250.50.140",
  "url": "http://httpbin.org/get?qry=你好嗎"
}
source
EasyCurl.http_headFunction
http_head(url::AbstractString; kw...) -> HTTPResponse

Shortcut for http_request function, work similar to http_request("HEAD", url; kw...).

Examples

julia> response = http_head(
           "http://httpbin.org/get";
           headers = Pair{String,String}[
               "Content-Type" => "application/json",
               "User-Agent" => "EasyCurl.jl",
           ],
           query = "qry=你好嗎",
           interface = "0.0.0.0",
       );

julia> http_body(response)
UInt8[]
source
EasyCurl.http_postFunction
http_post(url::AbstractString; kw...) -> HTTPResponse

Shortcut for http_request function, work similar to http_request("POST", url; kw...).

Examples

julia> response = http_post(
           "http://httpbin.org/post";
           headers = Pair{String,String}[
               "Content-Type" => "application/json",
               "User-Agent" => "EasyCurl.jl",
           ],
           query = "qry=你好嗎",
           body = "{"data":"hi"}",
       );

julia> http_body(response) |> String |> print
{
  "args": {
    "qry": "你好嗎"
  },
  "data": "{"data":"hi"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Length": "13",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "EasyCurl.jl",
    "X-Amzn-Trace-Id": "Root=1-66e0e208-4850928003a928fd230bfd59"
  },
  "json": {
    "data": "hi"
  },
  "origin": "100.250.50.140",
  "url": "http://httpbin.org/post?qry=你好嗎"
}
source
EasyCurl.http_putFunction
http_put(url::AbstractString; kw...) -> HTTPResponse

Shortcut for http_request function, work similar to http_request("PUT", url; kw...).

Examples

julia> response = http_put(
           "http://httpbin.org/put";
           headers = Pair{String,String}[
               "Content-Type" => "application/json",
               "User-Agent" => "EasyCurl.jl",
           ],
           query = "qry=你好嗎",
           body = "{"data":"hi"}",
       );

julia> http_body(response) |> String |> print
{
  "args": {
    "qry": "你好嗎"
  },
  "data": "{"data":"hi"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Length": "13",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "EasyCurl.jl",
    "X-Amzn-Trace-Id": "Root=1-66e0e239-34f8cfdc41164300719ce1f1"
  },
  "json": {
    "data": "hi"
  },
  "origin": "100.250.50.140",
  "url": "http://httpbin.org/put?qry=你好嗎"
}
source
EasyCurl.http_patchFunction
http_patch(url::AbstractString; kw...) -> HTTPResponse

Shortcut for http_request function, work similar to http_request("PATCH", url; kw...).

Examples

julia> response = http_patch(
           "http://httpbin.org/patch";
           headers = Pair{String,String}[
               "Content-Type" => "application/json",
               "User-Agent" => "EasyCurl.jl",
           ],
           query = "qry=你好嗎",
           body = "{"data":"hi"}",
       );

julia> http_body(response) |> String |> print
{
  "args": {
    "qry": "你好嗎"
  },
  "data": "{"data":"hi"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Length": "13",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "EasyCurl.jl",
    "X-Amzn-Trace-Id": "Root=1-66e0e266-5bd4762c619fbd642710bf3d"
  },
  "json": {
    "data": "hi"
  },
  "origin": "100.250.50.140",
  "url": "http://httpbin.org/patch?qry=你好嗎"
}
source
EasyCurl.http_deleteFunction
http_delete(url::AbstractString; kw...) -> HTTPResponse

Shortcut for http_request function, work similar to http_request("DELETE", url; kw...).

Examples

julia> response = http_delete(
           "http://httpbin.org/delete";
           headers = Pair{String,String}[
               "Content-Type" => "application/json",
               "User-Agent" => "EasyCurl.jl",
           ],
           query = "qry=你好嗎",
           body = "{"data":"hi"}",
       );

julia> http_body(response) |> String |> print
{
  "args": {
    "qry": "你好嗎"
  },
  "data": "{"data":"hi"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Content-Length": "13",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "EasyCurl.jl",
    "X-Amzn-Trace-Id": "Root=1-66e0e292-34acbab950035ea1763c3aca"
  },
  "json": {
    "data": "hi"
  },
  "origin": "100.250.50.140",
  "url": "http://httpbin.org/delete?qry=你好嗎"
}
source

Utilities

EasyCurl.http_iserrorFunction
http_iserror(x::HTTPResponse) -> Bool

Determines if the HTTP response indicates an error (status codes 400 and above).

Examples

julia> response = http_request("GET", "http://httpbin.org/get");

julia> http_iserror(response)
false
source
EasyCurl.http_headersFunction
http_headers(x::HTTPResponse, key::String) -> Vector{String}

Retrieve all values for a specific header field from a HTTPResponse object. This function is case-insensitive with regard to the header field name.

Examples

julia> response = http_request("GET", "http://httpbin.org/get");

julia> http_headers(response, "Content-Type")
1-element Vector{String}:
 "application/json"

julia> http_headers(response, "nonexistent-header")
String[]
source
EasyCurl.http_headerFunction
http_header(x::HTTPResponse, key::String, default = nothing)

Retrieve the first value of a specific header field by key from a HTTPResponse object. If the header is not found, the function returns a default value. This function is case-insensitive with regard to the header field name.

Examples

julia> response = http_request("GET", "http://httpbin.org/get");

julia> http_header(response, "Content-Type")
"application/json"

julia> http_header(response, "nonexistent-header", "default-value")
"default-value"
source
EasyCurl.HTTP_VERSION_MAPConstant
HTTP_VERSION_MAP::Dict{UInt64,String}

Maps CURL numerical constants for HTTP versions to their string representations.

  • 0x00000001 (CURL_HTTP_VERSION_1_0) - "1.0": HTTP 1.0
  • 0x00000002 (CURL_HTTP_VERSION_1_1) - "1.1": HTTP 1.1
  • 0x00000003 (CURL_HTTP_VERSION_2_0) - "2.0": HTTP 2.0
source