Synchronous REST API POST not working

  1.         obj = {}    
  2.         obj.nome = “Erica Palmeira”
  3.         local http = require(“socket.http”)
  4.         local ltn12 = require(“ltn12”)
  5.         local jsonData = json.encode(obj)
  6.         local params = {}
  7.         params.headers = {}
  8.        
  9.         params.headers[“application-id”] = “my app id”
  10.         params.headers[“secret-key”] = “my secret key”
  11.         params.headers[“Content-Type”] = “application/x-www-form-urlencoded” – x-www-form-urlencoded
  12.         params.headers[“application-type”] = “REST”
  13.         params.headers[“Content-Length”] = string.len(jsonData)
  14.         
  15.         URL = "https://api.backendless.com/v1/data/"…tabela
  16.        
  17.         response_body = {}
  18.         local body, code, headers = http.request{
  19.                             url = URL , 
  20.                             method = “POST”,
  21.                             headers = params.headers,
  22.                             source = ltn12.source.string(jsonData),
  23.                             sink = ltn12.sink.table(response_body)
  24.                             }
  25.         print("Body = ", body)
  26.         print("code = ", code)
  27.         print("headers = ", headers[1])
  28.         print("response body = ", response_body[1])

Its showing this error: {“message”:“Entity can’t be empty”,“code”:8011}

 

 

Whats wrong? How to get it working?

The socket.http module does not support HTTPS.

Rob

SOLVED

Just changed Content Type to JSON

Before:

params.headers[“Content-Type”] = “application/x-www-form-urlencoded”

After:

params.headers[“Content-Type”] = “application/json”

The socket.http module does not support HTTPS.

Rob

SOLVED

Just changed Content Type to JSON

Before:

params.headers[“Content-Type”] = “application/x-www-form-urlencoded”

After:

params.headers[“Content-Type”] = “application/json”