-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: how to curl get and post #103
Comments
@gitboy16 you'll want to first translate those examples to libcurl (the C library), and once you've done that, it translates directly to Julia. The example in the README file here shows how to do a basic GET. Adding headers requires code like this: slist = Ptr{Cvoid}(0)
for header in headers
slist = curl_slist_append(slist, header)
end
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist) To POST data you'd do something like this: curl_easy_setopt(curl, CURLOPT_POST, 1)
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, ncodeunits(requestBody))
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, requestBody) Specify the URL like this: curl_easy_setopt(curl, CURLOPT_URL, url) |
Thank you. res = curl_easy_perform(curl) I see a lot of data being printed. That is the data that I want in a string variable. |
you'll have to use a write callback to get data: 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)
# At this point your data is in `data` as a sequence of bytes. You can copy that into a global variable or something
sz::Csize_t
end
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, @cfunction(curl_write_cb, Csize_t, (Ptr{Cvoid}, Csize_t, Csize_t, Ptr{Cvoid}))) |
I am not sure to understand how to convert the data to string from the above example. Why not return the variable data from the above function? |
convert the data to string by using the You cannot return the variable from the above function because you don't call the function. Curl calls it internally and the return value goes to curl. You'll find a lot more information in the |
Ok thank you, I am going to search how to do that and post it here if I find out. Thanks again. |
might be worth to add some of this to the README |
You can use CurlHTTP.jl which makes it easier to use curl from Julia. |
Thanks @bluesmoon . I actually found this meanwhile. What I cannot find in CurlHTTP.jl is a way to add a cookie. Is this possible/difficult? |
Hi,
Is there an example on how to curl get and post using LibCURL.jl?
For example is it possible to do:
Or
The above example are directly taken from the following link (r package plumber):
https://www.rplumber.io/
Please note that I currently can't test the package HTTP because I currently can't install packages. I raise an issue in repo Pkg. The nice thing is that LibCURL comes pre-installed with Julia.
Thank you for your help.
The text was updated successfully, but these errors were encountered: