Logic Machine Forum
http post request issues - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: http post request issues (/showthread.php?tid=4820)



http post request issues - stork - 09.06.2023

I'm trying to send energy data through an API by using http.request:

Code:
local dateTime=os.date("%Y-%m-%dT%H:%M")

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

local data = grp.getvalue('360_001_OE001 - Heat energy E1')

body = [[{
"meterID": "0501e479-2a12-45b4-8cb6-505ced85428f",
"values": ["]]..dateTime..[[,]]..data..[[]
}]]

local response_body = {}

res, code, headers, status = http.request{
url = "https://www.energinet.net/apiImport/data",
method = "POST",
headers ={
["Authorization"] = "Bearer xxxxx",
["Content-Type"] = "application/json",
["Accept"] = "application/json"
},
source = ltn12.source.string(body),
sink = ltn12.sink.table(response_body)
}

log(response_body)
log(body)
log(res,code,headers,status)

log(res,code,headers, status) returns: 
* arg: 1 * nil * arg: 2 * string: Try again * arg: 3 * nil * arg: 4 * nil
response_body is empty.

Headers are as specified by the API-provider. 

Any ideas what might be wrong? Hiow should I understand the "Try again" from log(code)?


RE: http post request issues - admin - 09.06.2023

Check that gateway and DNS settings are valid in System config > Network > Interfaces > eth0.


RE: http post request issues - stork - 09.06.2023

They should be. The LM5 is online.


RE: http post request issues - Erwin van der Zwart - 09.06.2023

I think your body is not a valid json format, you are missing a " in the values property


RE: http post request issues - stork - 09.06.2023

Thanks for the replies. You are right, I saw that one, but had to add another DNS to be able to communicate. Also I had to add content-length even if it was not specified by the API-provider. Thanks for notifying me about DNS-settings, which was part of the problem.