# Writing data from curl request using LibCURL

**URL:** <https://discourse.julialang.org/t/writing-data-from-curl-request-using-libcurl/15276>\
**Category:** Web Stack\
**Tags:** question\
**Created:** [September 21, 2018, 1:42pm UTC](https://discourse.julialang.org/t/writing-data-from-curl-request-using-libcurl/15276 "2018-09-21T13:42:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julien\_Gooris](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@Julien\_Gooris](https://discourse.julialang.org/u/Julien_Gooris)\
**Post date:** [September 21, 2018, 1:42pm UTC](https://discourse.julialang.org/t/writing-data-from-curl-request-using-libcurl/15276/1 "2018-09-21T13:42:30Z")

</div>

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](https://github.com/JuliaWeb/LibCURL.jl) 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](http://example.com)”.  
Any idea how to solve this ?  
Thanks a lot !

---

<div class="post-metadata">

**Author:** ![bluesmoon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bluesmoon/32/327_2.png) [@bluesmoon](https://discourse.julialang.org/u/bluesmoon)\
**Post date:** [October 22, 2021, 3:55pm UTC](https://discourse.julialang.org/t/writing-data-from-curl-request-using-libcurl/15276/2 "2021-10-22T15:55:46Z")

</div>

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.

```julia
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](https://curl.se/libcurl/c/)

It is written for C as the LibCURL.jl library is a thin wrapper around the C library.
