Re: Error with _curl_
- Posted by jmduro Apr 21, 2018
- 1254 views
My bad! It took me long before I understood I didn't check the demo folder.
Here is an updated working demo. The previous one was adapted to Eu3.
-- CURL GET, PUT, POST, PATCH and DELETE -- usage for beginners -- sequences url, headers and body are expected already defined at this step include std/io.e include std/console.e include std/filesys.e include lib/_common_.e include lib/_curl_.e include lib/_curl_constants_.e object res atom curl f_debug = open(InitialDir & SLASH & "debug.log", "w") with_debug = 1 ------------------------------------------------------------------------------ -- common initialization ------------------------------------------------------------------------------ function init_curl_session() atom curl curl = curl_easy_init() log_printf("curl = %d\n", curl) if not curl then return 0 end if -- Proxy settings curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP) -- curl_easy_setopt(curl, CURLOPT_PROXY, "http://127.0.0.1:8888") -- ex: Fiddler2 proxy curl_easy_setopt(curl, CURLOPT_PROXY, "") -- no proxy -- Cookies settings -- beware of already registered cookies! -- if a connection fails, first remove or empty the cookie file -- before you verify your credentials if file_exists(InitialDir & SLASH & "cookies.txt") then write_file(InitialDir & SLASH & "cookies.txt", "") end if curl_easy_setopt(curl, CURLOPT_COOKIEFILE, InitialDir & SLASH & "cookies.txt") curl_easy_setopt(curl, CURLOPT_COOKIEJAR, InitialDir & SLASH & "cookies.txt") -- Common settings curl_easy_setopt(curl, CURLOPT_VERBOSE, 1) curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1) -- Skip Peer Verification curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0) -- Skip Host Verification curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0) return curl end function -------------------------------------------------------------------------------- -- CURL POST - Beginner -------------------------------------------------------------------------------- res = curl_global_init(CURL_GLOBAL_DEFAULT) curl = init_curl_session() if curl then res = curl_post(curl, "https://jsonplaceholder.typicode.com/posts", {"Content-type: application/json; charset=UTF-8"}, "{ \"title\": \"foo\", \"body\": \"bar\", \"userId\": 1 }") if (res[HTTP_STATUS] < 200) or (res[HTTP_STATUS] > 226) then puts(f_debug, "POST failed!\n" & res[HTTP_BODY] & "\n") end if puts(f_debug, "Result:\n" & res[HTTP_BODY] & "\n") curl_easy_cleanup(curl) end if curl_global_cleanup() -------------------------------------------------------------------------------- -- CURL PUT - Beginner -------------------------------------------------------------------------------- res = curl_global_init(CURL_GLOBAL_DEFAULT) curl = init_curl_session() if curl then res = curl_put(curl, "https://jsonplaceholder.typicode.com/posts/1", {"Content-type: application/json; charset=UTF-8"}, "{ \"id\": 1, \"title\": \"foo\", \"body\": \"bar\", \"userId\": 1 }") if (res[HTTP_STATUS] < 200) or (res[HTTP_STATUS] > 226) then puts(f_debug, "PUT failed!\n" & res[HTTP_BODY] & "\n") end if puts(f_debug, "Result:\n" & res[HTTP_BODY] & "\n") curl_easy_cleanup(curl) end if curl_global_cleanup() -------------------------------------------------------------------------------- -- CURL PATCH - Beginner -------------------------------------------------------------------------------- res = curl_global_init(CURL_GLOBAL_DEFAULT) curl = init_curl_session() if curl then res = curl_patch(curl, "https://jsonplaceholder.typicode.com/posts/1", {"Content-type: application/json; charset=UTF-8"}, "{ \"title\": \"foo\" }") if (res[HTTP_STATUS] < 200) or (res[HTTP_STATUS] > 226) then puts(f_debug, "PATCH failed!\n" & res[HTTP_BODY] & "\n") end if puts(f_debug, "Result:\n" & res[HTTP_BODY] & "\n") curl_easy_cleanup(curl) end if curl_global_cleanup() -------------------------------------------------------------------------------- -- CURL GET - Beginner -------------------------------------------------------------------------------- void = curl_global_init(CURL_GLOBAL_DEFAULT) curl = init_curl_session() if curl then res = curl_get(curl, "https://jsonplaceholder.typicode.com/posts/1", {"Content-type: application/json; charset=UTF-8"}) if (res[HTTP_STATUS] < 200) or (res[HTTP_STATUS] > 226) then puts(f_debug, "GET failed!\n" & res[HTTP_BODY] & "\n") end if puts(f_debug, "Result:\n" & res[HTTP_BODY] & "\n") curl_easy_cleanup(curl) end if curl_global_cleanup() -------------------------------------------------------------------------------- -- CURL DELETE - Beginner -------------------------------------------------------------------------------- res = curl_global_init(CURL_GLOBAL_DEFAULT) curl = init_curl_session() if curl then res = curl_delete(curl, "https://jsonplaceholder.typicode.com/posts/1", {"Content-type: application/json; charset=UTF-8"}) if (res[HTTP_STATUS] < 200) or (res[HTTP_STATUS] > 226) then puts(f_debug, "DELETE failed!\n" & res[HTTP_BODY] & "\n") end if puts(f_debug, "Result:\n" & res[HTTP_BODY] & "\n") curl_easy_cleanup(curl) end if curl_global_cleanup() puts(f_debug, "\nFinished\n") maybe_any_key()
Sorry for beeing slow on the uptake !
Jean-Marc