This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

API, POST
#1
So, I've run into a wall on something as ease as a POST function to a API.
How do I format the request to get this to work?

Code:
curl --request POST \
     --url https://api.easee.cloud/api/chargers/EH840488/settings \
     --header 'Authorization: Bearer *****MY TOKEN*****' \
     --header 'content-type: application/*+json' \
     --data '
{
     "dynamicChargerCurrent": 16
}
'
Code:
function WriteToEasee(request, request_body)
  request_url = 'https://api.easee.cloud/api/' .. request
  local response_body = {}
  local request_body = ""
  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Authorization"] = "Bearer " .. API_Token;
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  log(code)
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
    --API_Token = GetToken() -- request a new token
  end
end

dynCurr = 10
-- Dynamic current setting.
WriteToEasee('chargers/' .. ChargerID  .. '/settings', "dynamicChargerCurrent" .. dynCurr)
This is as far as I've gotten. I'm getting code 400, request is missing or has invalid values.
I guess it's a small matter, but i have not worked with POST functions before.
Reply
#2
Request is missing Content-Length header:
Code:
["Content-Length"] = #request_body,
Reply
#3
(01.11.2022, 07:19)admin Wrote: Request is missing Content-Length header:
Code:
["Content-Length"] = #request_body,

Still 400:
Code:
function WriteToEasee(request, request_body)
  request_url = 'https://api.easee.cloud/api/' .. request
    local response_body = {}
  local content_length

  if type(request_body) == "string" then
    content_length = #request_body
  else
    request_body = ""
  end

  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Authorization"] = "Bearer " .. API_Token;
      ["Content-Length"] = #request_body,
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  log(code)
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
    --API_Token = GetToken() -- request a new token
  end
end

dynCurr = 10
-- Dynamic current setting.
WriteToEasee('chargers/' .. ChargerID  .. '/settings', "dynamicChargerCurrent" .. dynCurr)

(01.11.2022, 07:23)tomnord Wrote:
(01.11.2022, 07:19)admin Wrote: Request is missing Content-Length header:
Code:
["Content-Length"] = #request_body,

Still 400:
Code:
function WriteToEasee(request, request_body)
  request_url = 'https://api.easee.cloud/api/' .. request
    local response_body = {}
  local content_length

  if type(request_body) == "string" then
    content_length = #request_body
  else
    request_body = ""
  end

  local body, code, hdrs, stat = ssl.https.request{
    url = request_url;
    method = "POST";
    headers =
    {
      ["Content-Type"] = "application/json";
      ["Authorization"] = "Bearer " .. API_Token;
      ["Content-Length"] = #request_body,
    };
    source = ltn12.source.string(request_body);
    sink = ltn12.sink.table(response_body);
  }
  log(code)
  if code == 200 then
    ret = table.concat(response_body)
    ret = json.pdecode(ret)
    return ret
  else
    --API_Token = GetToken() -- request a new token
  end
end

dynCurr = 10
-- Dynamic current setting.
WriteToEasee('chargers/' .. ChargerID  .. '/settings', "dynamicChargerCurrent" .. dynCurr)

Could it be a mismatch with this?
WriteToEasee('chargers/' .. ChargerID  .. '/settings', "dynamicChargerCurrent" .. dynCurr)

In the docs it says this:  "{\"dynamicChargerCurrent\":16}"
Reply
#4
You need to encode your request data using JSON:
Code:
body = require('json').encode({ dynamicChargerCurrent = dynCurr })
WriteToEasee('chargers/' .. ChargerID  .. '/settings', body)
Reply
#5
(01.11.2022, 07:27)admin Wrote: You need to encode your request data using JSON:
Code:
body = require('json').encode({ dynamicChargerCurrent = dynCurr })
WriteToEasee('chargers/' .. ChargerID  .. '/settings', body)

BINGO, Thank You, as always Smile
Only thing missing now is the response_body.. that one is blank.
Reply


Forum Jump: