Curl -d data url from Julia

I would like to do the equivalent of

run(`curl -d data url`)

from Julia, using LibCURL or one of the wrappers. Is there a simple way to do this?

(Motivation: curl may not be available on the host, but I have a working Julia environment.)

I think that CURL_jll.jl provides the curl executable.

So it might just be as simple as:

using CURL_jll: curl

run(`$(curl()) ...`)

(I haven’t tested this.)

1 Like

Any reason for not using Downloads.download?

Can I provide data to be sent with that?

using HTTP
headers = []
data = Dict("key" => "value")
HTTP.post(url, headers, data)

This should work.

Edit: curl seems to set a few headers by default, so you might want to consider

headers = ["Accept" => "*/*"]
1 Like

What should "key" be to be equivalent to data in curl -d data?

I assumed data was a placeholder. You can simply pass

HTTP.post(url, [], "data")
1 Like

Thanks, that works just great!

(In case someone is curious: I am using this at the end of a script to notify myself about the end of a computation with https://ntfy.sh/)

5 Likes

Thanks for this. I have been using Telegram (GitHub - Arkoniak/Telegram.jl: Native Julia Telegram bot api wrapper) for notifications. But it’s good to have other options.

I learned about nfty.sh in this topic, which I think lists all the feasible options. What I like about ntfy.sh is how lightweight the setup is — I just generate a random hex string, and use it as a channel with HTTP.