Skip to content
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

Open
gitboy16 opened this issue Apr 21, 2021 · 9 comments
Open

Question: how to curl get and post #103

gitboy16 opened this issue Apr 21, 2021 · 9 comments

Comments

@gitboy16
Copy link

Hi,
Is there an example on how to curl get and post using LibCURL.jl?
For example is it possible to do:

curl "http://localhost:8000/echo"

Or

curl -H "Content-Type: application /json" --data '{"a":4,"b":5}' http//localhost:800/sum

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.

@bluesmoon
Copy link

bluesmoon commented Oct 22, 2021

@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)

@gitboy16
Copy link
Author

gitboy16 commented Feb 1, 2022

Thank you.
When I run

res = curl_easy_perform(curl)

I see a lot of data being printed. That is the data that I want in a string variable.
How do I save these data in a string variable?
res does not seem to contain the data.
Thank you again

@bluesmoon
Copy link

bluesmoon commented Feb 1, 2022

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})))

@gitboy16
Copy link
Author

gitboy16 commented Feb 1, 2022

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?
Thank you

@bluesmoon
Copy link

convert the data to string by using the String() function.

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 curl documentation (not the julia docs, but the C library docs).

@gitboy16
Copy link
Author

gitboy16 commented Feb 1, 2022

Ok thank you, I am going to search how to do that and post it here if I find out. Thanks again.

@kafisatz
Copy link

kafisatz commented Oct 5, 2023

might be worth to add some of this to the README

@bluesmoon
Copy link

You can use CurlHTTP.jl which makes it easier to use curl from Julia.

@kafisatz
Copy link

kafisatz commented Oct 5, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants