[luci] Curl in LuCI?
Andrew Reynolds
andrew at opentechinstitute.org
Thu Jan 9 16:37:53 CET 2014
On 12/06/2013 11:51 AM, Helen Fornazier wrote:
> I see in LuCI many functions to parse http requests, but is there any
> function to perform an http GET or POST requests? As a client?
Hi Helen,
You can perform http GET or POST requests using luci.httpclient (package
luci-lib-httpclient). You will need one of the luci-lib-nixio-ssl
packages to do https requests.
I used the httpclient.request_raw() function, which requires you to
define your http headers and message body, including the form
parameters, then pass those values to the function.
Here's how I did it. Unfortunately I didn't find any httpclient
documentation, so this is just what worked for me through trial and error.
In this snippet, I am submitting a machine hostname (hostname) and some
json data (json_data) to a form on a central server (listener_url).
--[[
-- import httpclient
local https = require "luci.httpclient"
-- organize data
local hostname="my.host"
node_json="{string:value}"
-- build request
local listener_url="https://my.server/path_to_form"
local options={}
options.headers = {}
options.headers["Connection"] = "keep-alive"
options.headers["Content-Type"] = "application/x-www-form-urlencoded"
options.sndtimeo = '100'
options.rcvtimeo = '50'
-- options.method can also use "GET"
options.method = "POST"
options.message= {}
options.body = {}
-- structure: options.body.formid = formvalue
options.body.host = hostname
options.body.json = node_json
-- submit request
https.request_raw(listener_url, options)
]]--
Httpclient seems to have reasonable defaults, so you may not have to put
a lot of effort into defining your http headers. The meat of the form is
in options.body, where you just append key=value pairs corresponding to
the form IDs and values. Don't forget to include any hidden values.
Hope this helps!
-andrew
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 901 bytes
Desc: OpenPGP digital signature
URL: <http://lists.subsignal.org/pipermail/luci/attachments/20140109/18e22d78/attachment.pgp>
More information about the luci
mailing list