Re: https_get.e

new topic     » goto parent     » topic index » view thread      » older message » newer message
SDPringle said...

I bet you guys could create a https_post too, eh?

Will this work from JMDuro's libcurl4.e - curl_demo.ex:

 
-- from:libcurl4.e 
----------------------------------------------------------------- 
public function curl_post(atom curl, sequence url, sequence headers, 
                          object body) 
--<function> 
--<name>curl_post</name> 
--<digest>sends a POST 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_post(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", {{"output", 2}}) 
-- analyze_object(res[4], "Content", {{"output", 2}}) 
--</example> 
--<see_also>curl_easy_perform, curl_easy_perform_ex, curl_get, curl_put, curl_delete, curl_patch, curl_head</see_also> 
--</function> 
  sequence res 
  atom pheaders 
 
  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, 1) 
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body) 
 
  res = curl_easy_perform_ex(curl) 
  curl_slist_free_all(pheaders) 
  return res 
end function 
 
-------------------------------------------------------------------------------- 
 
-- from: curl_demo.ex 
-- 
-------------------------------------------------------------------------------- 
-- 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() 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu