Skip to content

Commit

Permalink
Add sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesmoon committed Mar 31, 2024
1 parent 8dc7a45 commit 96a48a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/multi-post.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CurlHTTP

const colors = [:light_red, :light_green, :light_yellow]
function make_handler(i::Int)
return ch -> Base.printstyled("Handle $i: ", String(take!(ch)), "\n"; color=colors[i])
end

pool = map(1:3) do i
curl = CurlEasy(url="https://postman-echo.com/post?val=$i", method=CurlHTTP.POST)
requestBody = """{"testName":"test_multiPOST","value":$i}"""

curl.userdata[:index] = i # userdata is a Dict to store anything you want
curl.userdata[:channel] = Channel(make_handler(i), Inf)

CurlHTTP.curl_setup_request(curl, requestBody, ["Content-Type: application/json"];
data_channel = curl.userdata[:channel]
)
return curl
end

# Set up tasks to handle the data channels before calling curl_execute

multi = CurlMulti(pool)
res = curl_execute(multi)
23 changes: 23 additions & 0 deletions examples/simple-get.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CurlHTTP

const NoBody = ""

curl = CurlEasy(
url="https://postman-echo.com/get?foo=bar&baz=zod",
method=CurlHTTP.GET,
)

databuffer = UInt8[]

res, http_status, errormessage = curl_execute(curl, NoBody, ["X-Custom-Header: ding"]) do d
append!(databuffer, d)
end

data = JSON3.read(databuffer, Dict{String, Any})


# We can reuse this connection
res, http_status, errormessage = curl_execute(curl, NoBody, ["X-Custom-Header: ding2"]; url="https://postman-echo.com/get?foo=bear&baz=zeroed")
data = JSON3.read(curl.userdata[:databuffer], Dict{String, Any})

curl_cleanup(curl) # Optional - will run on finalizer

0 comments on commit 96a48a7

Please sign in to comment.