Hi all,
I (as Julia Newbie) am trying to execute some Curl requests using LibCURL and to fetch the response content (not only the http code) into a string variable. I’m following the instructions from GitHub - JuliaWeb/LibCURL.jl: Julia wrapper for libcurl but in the end I cannot not access (or don’t know how to access, while it is printed in the REPL) the HTML content from “http://example.com”.
Any idea how to solve this ?
Thanks a lot !
LibCURL is a fairly low level library, so you will need to write your own handler function to receive and convert the data from the C format.
function curl_write_cb(curlbuf::Ptr{Cvoid}, s::Csize_t, n::Csize_t, p_ctxt::Ptr{Cvoid})::Csize_t
sz = s * n
data = Array{UInt8}(undef, sz)
ccall(:memcpy, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, UInt64), data, curlbuf, sz)
if p_ctxt == C_NULL
j_ctxt = nothing
else
j_ctxt = unsafe_pointer_to_objref(p_ctxt)
end
append!(j_ctxt, data)
sz
end
curl = curl_easy_init()
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @cfunction(curl_write_cb, Csize_t, (Ptr{Cvoid}, Csize_t, Csize_t, Ptr{Cvoid})))
buffer = UInt8[]
curl_easy_setopt(curl, CURLOPT_WRITEDATA, pointer_from_objref(buffer))
curl_easy_setopt(curl, CURLOPT_URL, url)
GC.@preserve buffer begin
res = curl_easy_perform(curl)
end
# At this point buffer will contain all the data read from the network
To learn more, you’ll need to read the CURL API docs at libcurl - API
It is written for C as the LibCURL.jl library is a thin wrapper around the C library.