Logic Machine Forum
JSON post request being weird - 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: JSON post request being weird (/showthread.php?tid=4979)



JSON post request being weird - Seijboldt - 20.09.2023

Hi,
I've garbled a script I found, but for some reason my API is refusing the JSON content.
Maybe I've got a typo or I'm just slow..

Here is my current code:
Code:
require('socket.http')
require('json')
require("ltn12")
body = '{"fault": true,"instID": "string","value": 0,"name": "string"}'
response = {}
start = grp.getvalue('12/4/0')

socket.http.request({
      url = "http://192.168.1.69/docs",
   method = 'POST',
     sink = ltn12.sink.table(response),
  headers = {
        ['accept'] = 'application/json',
     ['content-type'] = 'application/json',
            },
      source = ltn12.source.string(body),
        })
log(response,headers)
That results in this: 
Code:
string: {"detail":[{"type":"missing","loc":["body"],"msg":"Field required","input":null}]}


Which I could probably live with as it's just missing some or all values in the JSON format, but I decided to try it through CURL and that works and returns 201 success:
Code:
curl -X 'POST' \
  'http://192.168.1.69/docs' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{"fault": true,"instID": "string","value": 0,"name": "string"}'
I've tried enclosing "true" and "0" in " but that doesn't make a difference.


Any ideas would be greatly appreciated.


RE: JSON post request being weird - admin - 21.09.2023

You are missing the content-length header. It is also advisable to use json.encode to create JSON strings instead of manual creation which can lead to errors.


RE: JSON post request being weird - Seijboldt - 21.09.2023

(21.09.2023, 05:06)admin Wrote: You are missing the content-length header. It is also advisable to use json.encode to create JSON strings instead of manual creation which can lead to errors.

I've added the header, with it in I get a internal server error Sad 

This should be correct, right?
Code:
mac = io.readfile('/sys/class/net/eth0/address'):trim()
require('socket.http')
require('json')
require("ltn12")
jsoncontent = json.encode({fault = true,instID= "teststring",value= 22,name = "stringname"})

body = jsoncontent
response = {}
log("test")

socket.http.request({
      url = "http://192.168.1.69/docs",
   method = 'POST',
     sink = ltn12.sink.table(response),
  headers = {
     ["Content-Length"] = string.len(body),
        ['accept'] = 'application/json',
     ['content-type'] = 'application/json',
            },
      source = ltn12.source.string(body),
        })
log(response,body)



RE: JSON post request being weird - admin - 21.09.2023

Check error logs on your server. This request works correctly when testing with https://httpbin.org/post


RE: JSON post request being weird - Seijboldt - 22.09.2023

(21.09.2023, 08:28)admin Wrote: Check error logs on your server. This request works correctly when testing with https://httpbin.org/post

You are correct, had a imported library on my server that made some issues.