Client Requests
EasyCurl.curl_request
— Functioncurl_request(method::AbstractString, url::AbstractString; kw...) -> EasyCurl.Response
Send a url
HTTP Request using as method
one of "GET"
, "POST"
, etc. and return a EasyCurl.Response
object.
Keyword arguments
headers = Pair{String,String}[]
: The headers for the request.body = nothing
: The body for the request.query = nothing
: The query string for the request.interface = nothing
: The interface for the request.status_exception = true
: Whether to throw an exception if the response status code indicates an error.connect_timeout = 60
: The connect timeout for the request in seconds.read_timeout = 300
: The read timeout for the request in seconds.retries = 1
: The number of times to retry the request if an error occurs.proxy = nothing
: Which proxy to use for the request.accept_encoding = "gzip"
: Encoding to accept.verbose::Bool = false
: Enables verbose output from EasyCurl for debugging.ssl_verifypeer = true
: Whether peer need to be verified.
Examples
julia> headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json"
]
julia> response = curl_request("POST", "http://httpbin.org/post", headers = headers, query = "qry=你好嗎",
body = "{\"data\":\"hi\"}", interface = "en0", read_timeout = 5, connect_timeout = 10, retries = 10)
julia> curl_status(response)
200
julia> curl_body(response) |> String |> print
{
"headers": {
"X-Amzn-Trace-Id": "Root=1-6588a009-19f3dc0321bee38106226bb3",
"Content-Length": "13",
"Host": "httpbin.org",
"Accept": "*/*",
"Content-Type": "application/json",
"Accept-Encoding": "gzip",
"User-Agent": "EasyCurl.jl"
},
"json": {
"data": "hi"
},
"files": {},
"args": {
"qry": "你好嗎"
},
"data": "{\"data\":\"hi\"}",
"url": "http://httpbin.org/post?qry=你好嗎",
"form": {},
"origin": "100.250.50.140"
}
EasyCurl.curl_get
— Functioncurl_get(url::AbstractString; kw...) -> EasyCurl.Response
Shortcut for curl_request
function, work similar to curl_request("GET", url; kw...)
.
Examples
julia> headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json"
]
julia> response = curl_get("http://httpbin.org/get", headers = headers,
query = Dict{String,String}("qry" => "你好嗎"))
julia> curl_status(response)
200
julia> curl_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-6589e259-24815d6d62da962a06fc7edf"
},
"origin": "100.250.50.140",
"url": "http://httpbin.org/get?qry=你好嗎"
}
EasyCurl.curl_put
— Functioncurl_put(url::AbstractString; kw...) -> EasyCurl.Response
Shortcut for curl_request
function, work similar to curl_request("PUT", url; kw...)
.
Examples
julia> headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json"
]
julia> response = curl_put("http://httpbin.org/put", headers = headers,
query = "qry=你好嗎", body = "{\"data\":\"hi\"}")
julia> curl_status(response)
200
julia> curl_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-6589e3b0-58cdde84399ad8be30eb4e46"
},
"json": {
"data": "hi"
},
"origin": "100.250.50.140",
"url": "http://httpbin.org/put?qry=你好嗎"
}
EasyCurl.curl_post
— Functioncurl_post(url::AbstractString; kw...) -> EasyCurl.Response
Shortcut for curl_request
function, work similar to curl_request("POST", url; kw...)
.
Examples
julia> headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json"
]
julia> response = curl_post("http://httpbin.org/post", headers = headers,
query = "qry=你好嗎", body = "{\"data\":\"hi\"}")
julia> curl_status(response)
200
julia> curl_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-6589e32c-7f09b85d56e11aea59cde1d6"
},
"json": {
"data": "hi"
},
"origin": "100.250.50.140",
"url": "http://httpbin.org/post?qry=你好嗎"
}
EasyCurl.curl_head
— Functioncurl_head(url::AbstractString; kw...) -> EasyCurl.Response
Shortcut for curl_request
function, work similar to curl_request("HEAD", url; kw...)
.
Examples
julia> headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json"
]
julia> response = curl_head("http://httpbin.org/get", headers = headers,
query = "qry=你好嗎", interface = "0.0.0.0")
julia> curl_status(response)
200
julia> curl_body(response)
UInt8[]
EasyCurl.curl_patch
— Functioncurl_patch(url::AbstractString; kw...) -> EasyCurl.Response
Shortcut for curl_request
function, work similar to curl_request("PATCH", url; kw...)
.
Examples
julia> headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json"
]
julia> response = curl_patch("http://httpbin.org/patch", headers = headers,
query = "qry=你好嗎", body = "{\"data\":\"hi\"}")
julia> curl_status(response)
200
julia> curl_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-6589e410-33f8cb5a31db9fba6c0a746f"
},
"json": {
"data": "hi"
},
"origin": "100.250.50.140",
"url": "http://httpbin.org/patch?qry=你好嗎"
}
EasyCurl.curl_delete
— Functioncurl_delete(url::AbstractString; kw...) -> EasyCurl.Response
Shortcut for curl_request
function, work similar to curl_request("DELETE", url; kw...)
.
Examples
julia> headers = Pair{String,String}[
"User-Agent" => "EasyCurl.jl",
"Content-Type" => "application/json"
]
julia> response = curl_delete("http://httpbin.org/delete", headers = headers,
query = "qry=你好嗎", body = "{\"data\":\"hi\"}")
julia> curl_status(response)
200
julia> curl_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-6589e5f7-1c1ff2407f567ff17786576d"
},
"json": {
"data": "hi"
},
"origin": "100.250.50.140",
"url": "http://httpbin.org/delete?qry=你好嗎"
}
Request/Response types
EasyCurl.Request
— TypeRequest
Represents an HTTP request object.
Fields
method::String
: The HTTP request method (e.g."GET"
,"POST"
, etc.).url::String
: The URL to which the request is sent.headers::Vector{Pair{String, String}}
: Headers for the HTTP request.body::Vector{UInt8}
: The request body as a vector of bytes.connect_timeout::Real
: The connection timeout for the request in seconds.read_timeout::Real
: The read timeout for the response in seconds.interface::Union{String, Nothing}
: The network interface to use (ornothing
for the default).proxy::Union{String, Nothing}
: The proxy server to use (ornothing
for no proxy).accept_encoding::Union{String,Nothing}
: The accepted encoding for the response (e.g., "gzip").ssl_verifypeer::Bool
: Whether to verify SSL certificates.verbose::Bool
: Enables verbose output from EasyCurl for debugging.rq_curl::Ptr{CURL}
: A pointer to a EasyCurl handle for the request.rq_multi::Ptr{CURL}
: A pointer to a EasyCurl multi handle for the request.response::CurlResponse
: The HTTP response associated with this request.
EasyCurl.Response
— TypeEasyCurl.Response(x::CurlResponse)
Represents an HTTP response object that can be received from a CurlResponse
.
Fields
status::Int64
: The HTTP status code of the response.request_time::Float64
: The time taken for the HTTP request in seconds.headers::Vector{Pair{String,String}}
: Headers received in the HTTP response.body::Vector{UInt8}
: The response body as a vector of bytes.
See also: curl_status
, curl_request_time
, curl_headers
, curl_body
, curl_iserror
Advanced Topics
EasyCurl.curl_status
— Functioncurl_status(x::Response) -> Int64
Extracts the HTTP status code from a EasyCurl.Response
object.
EasyCurl.curl_request_time
— Functioncurl_request_time(x::Response) -> Float64
Extracts the request time from a EasyCurl.Response
object.
EasyCurl.curl_headers
— Functioncurl_headers(x::Response) -> Vector{Pair{String,String}}
Parses the HTTP headers from a EasyCurl.Response
object.
EasyCurl.curl_body
— Functioncurl_body(x::Response) -> Vector{UInt8}
Extracts the response body from a EasyCurl.Response
object.
EasyCurl.curl_iserror
— Functioncurl_iserror(x::Response) -> Bool
Check that EasyCurl.Response
have an error status
EasyCurl.joinurl
— FunctionEasyCurl.joinurl(basepart::AbstractString, parts::AbstractString...)::String
Construct a URL by concatenating a base part with one or more path segments. This function ensures that each segment is separated by a single forward slash (/
), regardless of whether the basepart
or parts
already contain slashes at their boundaries.
Examples
julia> EasyCurl.joinurl("http://example.com", "path")
"http://example.com/path"
julia> EasyCurl.joinurl("http://example.com/", "/path/to/resource")
"http://example.com/path/to/resource"
Encode/decode
EasyCurl.urlencode
— FunctionEasyCurl.urlencode(s::AbstractString)
Encode a string s
into URI using only the US-ASCII characters legal within a URI.
Examples
julia> EasyCurl.urlencode("[curl]")
"%5Bcurl%5D"
EasyCurl.urldecode
— FunctionEasyCurl.urldecode(s::AbstractString)
Decode an encoded URI string s
back to normal string.
Examples
julia> EasyCurl.urldecode("%5Bcurl%5D")
"[curl]"