Edge Driver HTTP Close when Call HTTP Request

Hello there.

I’m making an HTTP request by changing the example of esp8266.

The following is a common function.

------------------------
-- Send LAN HTTP Request
function command_handler.send_lan_command(url, method, path)
  local dest_url = url..'/'..path
  local res_body = {}

  log.trace('request to ' ..dest_url..' method ' ..method)

  -- HTTP Request
  local _, code = http.request({
    method=method,
    url=dest_url,
    sink=ltn12.sink.table(res_body),
    headers={
    }})

  log.trace('response code ' ..code)

  -- Handle response
  if code == 200 then
    return true, res_body
  end
  return false, nil
end

The function request is performed as follows.

  local success, data = command_handler.send_lan_command(
    device.device_network_id,
    'GET',
    'data')

The device_network_id is entered as follows.
ex) http://192.168.0.12:80

therefor log msg in log.trace(‘request to ’ …dest_url…’ method ’ …method) shown like

request to http://192.168.1.12:80/data method GET

In this state, the following errors are continuously displayed.

response code [string “socket”]:1406: closed

I’d like to get advice on this matter.

Regards.

Welcome to the SmartThings Community, @ktj1312!

Are you sending this request to an external device or is it a request within the driver?
If it’s for an external device, do you get that error before receiving the request on the device?
If it’s a request within the driver, please check my post here, there are some changes we need to apply to send internal requests.

Hi there.
I don’t understand what is external device mean exactly.
Here is my network configuration.
Router IP 192.168.1.1
ST Hub 192.168.1.71
Target Device 192.168.1.82

I don’t understand

mean.

for detail I use the function like this.

------------------------
-- Update Device Status
function command_handler.update_device_status(device)
  -- Refresh Device Status
  log.trace('Refreshing Device Status')

  local success, data = command_handler.send_lan_command(
    device.device_network_id,
    'GET',
    'data')

    log.trace(data)

  -- Check success
  if success then
    log.trace(data)
    local resp = json.decode(table.concat(data)..'}')
    return true
  else
    log.error('failed to poll device state')
    return false
  end
end

and blow error

show at this line.

I meant it was a command sent from the Hub to another device like the ESP8266 because other developers have given the sample other purposes.

As I understand per your description, you send the command to the device (192.168.1.82) and it is received there but when it returns a response, the driver shows the message response code [string “socket”]:1406: closed

Have you checked how long does it take for that error to appear? This is to check if it wasn’t caused by a timeout in the response.

I am also seeing the same error when sending a http request to one of my house devices.
The strange thing is that it works fine when sending the same http request from Postman to the device.

Can you use a browser to issue your request successfully?

You may be missing required headers (HOST, ACCEPT, etc)

Timeout is immediately output in the log.
If I put the keep-alive attribute in the header, the timeout will be displayed after a certain period of time.

same issue with me.

The response is checked normally in the browser.
Postman also checks the response normally.

Is there any other way to check how the ST Hub sends requests except to run a separate server?

I highly suspect missing headers in your HTTP request. Use postman or google chrome developer tools to display the headers that are being sent with your request and you’ll see there are many for those tools that are working OK… Host and Accept are the common ones expected by most servers.

You can take the relevant parts of your Lua code and run it on any machine that has Lua 5.3 installed. Just change the cosock requires to regular socket.

I opened another web server to see how the request came in from the hub.
The request to the web server is performed normally. (HTTP 200)

The device that I’m trying to connect is responds normally in my browser. But it doesn’t work on Edge driver. Now, I think we need to check why the existing device is not responding.

Please give me advice on which part to check.

Are you sure the device ok with http and not https?

I would add an Accept header with the data type you are expecting back from the device.

And maybe try removing the connection and te headers.

HTTP request is correct.
I added an accept header.

Close and te headers are supposed to be automatically added to the 40x library.

Is there a way to delete the header value?

I didn’t realize they were being added by the library, so I guess you should leave them alone. I don’t know what else to suggest!

What type of device are you sending these requests to?

@ktj1312 @Tim99

Were either of you able to ever resolve the HTTP error you were getting? It turns out now I have a similar problem as well. Although my error is slightly different:

[string “socket”]:1410: closed

There is a preceding HTTP request before this occurs that always completes with no error. (same device, different message) But when it tries this next message, I get the error. I have numerous retries in my code and they only occasionally return with no error. The strange thing is, the device itself shows that it, in fact, DID get the message because its settings got changed as a result of the message. So somehow the problem is with the response message.

I am wondering if the device isn’t closing the connection so quickly after it sends its response, that the hub’s socket layer code isn’t fast enough to process the message before the connection is closed? It’s very frustrating! I think I am going to try and put a TCP trace on to see better what is happening…