[solved]Problem with http post request

Hope i am posting in correct area.

I am having issues sending a https request via a scene using open luup.

The server i am trying to post to will only recieve post request in web form or or json body or cURL.

I am using method 3 here http://wiki.micasaverde.com/index.php/Luup_Scenes_Events#Access_the_web

It works fine using this :

local http = require("socket.http")
  http.TIMEOUT = 5
  result, status = http.request("https://maker.ifttt.com/trigger/##1##/with/key/ghXkfhheysjswjs7sjhdfhdfhgsgpW") 

but if i try:

local http = require("socket.http")
  http.TIMEOUT = 5
  result, status = http.request("https://llamalab.com/automate/cloud/message?secret=uf7d8dfhfj43h4h3heyyh43h3usu&to=test@test.com&device=null&priority=high&payload=launch;com.test.test;test;")

it is not recieved.

It all works from automate test page here https://llamalab.com/automate/cloud/

Can anyone help me out. Is the request method actuall post?

No, it’s not a POST, it’s a GET.

See documentation for the LuaSocket library here:

http://w3.impa.br/~diego/software/luasocket/http.html#request

You need to use the two parameter version of the request call.

local http = require("socket.http")
  http.TIMEOUT = 5
  result, status = http.request("https://llamalab.com/automate/cloud/message", "secret=uf7d.8dfhfj43h4h3heyyh43h3usu=&to=test@test.com&device=null&priority=high&payload=launch;com.test.test;test;")

like this, i have tried too but no result

What status return do you get from the request?

I just tried this code snippet, changing the URL to a local test machine which echos the parameters it receives from a request. It responds with this:

{
  "*HEADER:Connection":"close, TE",
  "*HEADER:Content-Length":"115",
  "*HEADER:Content-Type":"application/x-www-form-urlencoded",
  "*HEADER:Host":"172.16.42.126",
  "*HEADER:Te":"trailers",
  "*HEADER:User-Agent":"LuaSocket 2.0.2",
  "CONTENT_LENGTH":"115",
  "CONTENT_TYPE":"application/x-www-form-urlencoded",
  "HTTP_USER_AGENT":"LuaSocket 2.0.2",
  "PATH_INFO":"/",
  "POST_CONTENT":"",
  "REMOTE_HOST":"172.16.42.126",
  "REQUEST_METHOD":"POST",
  "SCRIPT_NAME":"/cgi/test.lua",
  "SERVER_PROTOCOL":"HTTP/1.1",
  "TEST":"table: 0x03c984b0",
  "error":"table: 0x03c983e8",
  "input":"table: 0x03c98328",
  "params":[],
  "req":{
    "GET":[],
    "POST":{
      "device":"null",
      "payload":"launch;com.test.test;test;",
      "priority":"high",
      "secret":"uf7d.8dfhfj43h4h3heyyh43h3usu",
      "to":"test@test.com"
    },
    "app_path":"",
    "cookies":[],
    "doc_root":"",
    "env":"table: 0x03c98488",
    "method":"POST",
    "params":[],
    "parse_post":"function: 0x03dea398",
    "path_info":"/",
    "query_string":"",
    "script_name":"/cgi/test.lua"
  }
} 

So it all looks OK.

1 Like

cheers akbooer.
must be somthing in the headers that causing the server to reject the request.

the secret is missing the last character

uf7d.8dfhfj43h4h3heyyh43h3usu

should read

uf7d.8dfhfj43h4h3heyyh43h3usu=

how would i escape that character?

edit/
Well it’s not that have changed secret and format is this : “2.2CboAxRfbtqvDcFz3MhGPKmNgwayBfSnMfz7zL_02EQ”

Again, the status response should give you a clue as to what is wrong.

How do i get a status from a test luup code in the app developer section?

Well, you could write it to he log using luup.log()

…but if you were using the AltUI plugin, you could just use the print statement.

solved, Cheers akbooer

50	12/24/19 18:16:19.093	luup_log:0: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="robots" content="noindex, nofollow" />
<title>Automate Cloud Endpoint</title>
<link href="/favicon.ico" type="image/vnd.microsoft.icon" rel="icon" />
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container"><div class="row"><div class="col-md-12">
<h1>Message sent</h1>
</div></div></div>
</body>
</html>
 <0x724de520>```

Hi once again.

i have been trying to send post request as json body using

curl, response, http = luup.inet.request { 
    url = "http://192.168.1.25:1880/hello",
    data = {["My parameter"] = "Some value"},
    headers = {accept = "application/json"}
}

info at http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_request

the request is responding 200, but the json is not being passed correctly. the received data is

[45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,102,53,50,99,52,52,48,48,102,56,50,56,52,102,99,50,13,10,67,111,110,116,101,110,116,45,68,105,115,112,111,115,105,116,105,111,110,58,32,102,111,114,109,45,100,97,116,97,59,32,110,97,109,101,61,34,77,121,32,112,97,114,97,109,101,116,101,114,34,13,10,13,10,83,111,109,101,32,118,97,108,117,101,13,10,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,45,102,53,50,99,52,52,48,48,102,56,50,56,52,102,99,50,45,45,13,10]

Are you using openLuup, or Vera?

I’ve heard that the luup.inet.request() function is flakey on Vera, and neither is it well tested in openLuup (it is a recent edition to the luup library.) See:

I was trying on veraplus.

I have tried multiple codes i have found all over this forum. They ethier fail to run or run but no data is received. i have editted out quote and replaced . i can not get any examples of the os.execute()to work.

here’s another script i have messed with to no avail.

local http = require("socket.http")
local ltn12 = require("ltn12")

function sendRequest()
local path = "http://192.168.1.25:1880/hello"
  local payload = [[ {"key":"My Key","name":"My Name","description":"The description","state":1} ]]
  local response_body = { }

  local res, code, response_headers, status = http.request
  {
    url = path,
    method = "POST",
    headers =
    {
      ["Content-Type"] = "application/json",
      ["Content-Length"] = payload:len()
    },
    source = ltn12.source.string(payload),
    sink = ltn12.sink.table(response_body)
  }

Ok i got this to work have to escape all the ’ quotes.

os.execute('curl -X POST -H \'content-type:application/json\' -d \'{"testpc":"2","0d":"1"}\' http://192.168.1.25:1880/hell')

its very sluggish, and only works intermittently.

Why is this simple task so difficult? i’ve spent hours to day reading and trying different ideas with no reliable outcome.

I’ve tried this Lua script myself, verbatim, with the addition of a missing end statement and changing the URL to run a test CGI on my development machine.

The CGI returns a text representation of the request it receives, including headers and a post data, and gives me this:

{
  "*HEADER:Connection":"close, TE",
  "*HEADER:Content-Length":"77",
  "*HEADER:Content-Type":"application/json",
  "*HEADER:Host":"172.16.42.126",
  "*HEADER:Te":"trailers",
  "*HEADER:User-Agent":"LuaSocket 2.0.2",
  "CONTENT_LENGTH":"77",
  "CONTENT_TYPE":"application/json",
  "HTTP_USER_AGENT":"LuaSocket 2.0.2",
  "PATH_INFO":"/",
  "POST_CONTENT":"",
  "REMOTE_HOST":"172.16.42.126",
  "REQUEST_METHOD":"POST",
  "SCRIPT_NAME":"/cgi/test.lua",
  "SERVER_PROTOCOL":"HTTP/1.1",
  "TEST":"table: 0x03606d00",
  "error":"table: 0x03462998",
  "input":"table: 0x00563ad8",
  "params":[],
  "req":{
    "GET":[],
    "POST":{"post_data":" {\"key\":\"My Key\",\"name\":\"My Name\",\"description\":\"The description\",\"state\":1} "},
    "app_path":"",
    "cookies":[],
    "doc_root":"",
    "env":"table: 0x034ee8b0",
    "method":"POST",
    "params":[],
    "parse_post":"function: 0x0327e3b8",
    "path_info":"/",
    "query_string":"",
    "script_name":"/cgi/test.lua"
  }
}

So it works fine.

This really should be the best way to achieve what you need, without the delays involved in executing an external process.

1 Like

Strange, I had the end on there, just missed it when i copied it.

I have just tried it again and the code fails to run on my veraplus.

I did find some code here POST request for PushBullet that i managed to get to work with minor edits

Worth checking that Lua code is not disabled as a security feature. Also, for further diagnostics look at the return values from the request call.

Or maybe you’re happy with what you’ve got.

went with this in the end as i wanted to use https

local https = require "ssl.https"
local http = require "socket.http"
local ltn12 = require "ltn12"
local json = require "dkjson"
local payload = {{"test", "test", null, 0}} 

local request_body = { secret = "asecret", to = "test0@gmail.com", device = null, priority = "high", payload = payload }
local response_body = {}
request_body = json.encode(request_body)

local r, c, h, s = https.request {
  url = 'https://llamalab.com/automate/cloud/message',
  method = 'POST',
  headers = {
    ["Content-Type"] = "application/json",
    ["Content-Length"] = string.len(request_body)
  },
  source = ltn12.source.string(request_body),
  sink = ltn12.sink.table(response_body)
}

akbooer it was the secrity setting run unsafe lua. Strange how one script worked with setting unchecked, but other didn’t.

1 Like

© 2020 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy