Re: Hitting an API with Euphoria
- Posted by jmduro Dec 24, 2017
- 1740 views
In my last Standard library, I added a demo to show REST API management with libcurl. It is not yet ready for use. On published 1.0.5 version, it needs a little change in _curl_.e:
In curl_easy_setopt, the lines corresponding to slists have to be changed this way:
) then -- struct curl_slist * code = exec_func(xcurl_easy_setopt, {curl, option, param}, "+curl_easy_setopt")
I also added a curl_put function:
global function curl_put(atom curl, sequence url, sequence headers, object body) --<function> --<name>curl_put</name> --<digest>sends a PUT request to an URL and gets the page</digest> --<desc> -- intended to be used with REST APIs --</desc> --<param> --<type>atom</type> --<name>handle</name> --<desc>CURL session handle</desc> --</param> --<param> --<type>sequence</type> --<name>url</name> --<desc>URL to get the page from</desc> --</param> --<param> --<type>sequence</type> --<name>headers</name> --<desc>list of request headers</desc> --</param> --<param> --<type>object</type> --<name>body</name> --<desc>body of the request. Either a string or NULL.</desc> --</param> --<return> -- sequence -- * status : HTTP status -- * url : effective URL (useful if redirection is followed) -- * headers : sequence of headers -- * content : HTML Page content --</return> --<example> -- constant DEFAULT_HEADERS = { -- "Host: 192.168.1.10", -- "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0 " & -- "Gecko/20100101 Firefox/45.0", -- "Accept: application/json" -- } -- -- object res = curl_put(curl, MY_REST_API & "/session/" & sessionId & -- "/timeouts/implicit_wait", DEFAULT_HEADERS, "{" & -- "\"sessionId\": \"" & sessionId & "\", " & -- sprintf("\"ms\": %d", ms) & -- "}") -- printf(1, "Status: %d\n", {res[1]}) -- printf(1, "Effective URL: %s\n", {res[2]}) -- analyze_object(res[3], "Headers", f_debug) -- analyze_object(res[4], "Content", f_debug) --</example> --<see_also>curl_easy_perform, curl_easy_perform_ex, curl_get, curl_post, curl_delete, curl_patch</see_also> --</function> sequence res atom pheaders log_puts("\nPUT " & object_dump(url) & " " & object_dump(headers) & " " & object_dump(body) & "\n") curl_easy_setopt(curl, CURLOPT_URL, url) pheaders = NULL for i = 1 to length(headers) do pheaders = curl_slist_append(pheaders, headers[i]) end for curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pheaders) curl_easy_setopt(curl, CURLOPT_POST, 0) curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body) curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT") res = curl_send(curl) curl_slist_free_all(pheaders) return res end function
Here is the demo:
include get.e include lib/_curl_.e object res atom curl f_debug = 1 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(1, "POST failed!\n" & res[HTTP_BODY] & "\n") end if puts(1, "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(1, "PUT failed!\n" & res[HTTP_BODY] & "\n") end if puts(1, "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(1, "PATCH failed!\n" & res[HTTP_BODY] & "\n") end if puts(1, "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(1, "GET failed!\n" & res[HTTP_BODY] & "\n") end if puts(1, "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(1, "DELETE failed!\n" & res[HTTP_BODY] & "\n") end if puts(1, "Result:\n" & res[HTTP_BODY] & "\n") curl_easy_cleanup(curl) end if curl_global_cleanup() puts(1, "\nFinished\n") res = wait_key()
Jean-Marc