openwisp-controller icon indicating copy to clipboard operation
openwisp-controller copied to clipboard

rest api to send command seems doesn't work

Open sergorl opened this issue 3 years ago • 3 comments

My curl command looks like:

curl -X POST "https://openwisp.controller.io/api/v1/controller/device/8cb65d82-dfd8-4a36-94ca-206aec315494/command/" -H "accept: application/json" -H  "Authorization: Bearer "some token"" -H  "X-CSRFToken: lhYJJWLgB53saQaOZXWMSxYSe38pAghCHFivFYW9TuOpBGFi9ubE2s1OIfplDoGh" -d "{  \"input\": \"echo \"Hello from OpeWISP\" >> /data/data.txt\",  \"connection\": \"ca39961c-d3ca-4e0a-892f-637d29468007\",  \"type\": \"custom\"}"

But I got this: {"input":["This field is required."],"type":["This field is required."]}

what am I doing wrong?

sergorl avatar Jul 08 '22 13:07 sergorl

I change on -H "Content-Type: application/json"

curl -X POST "https://openwisp.controller.io/api/v1/controller/device/8cb65d82-dfd8-4a36-94ca-206aec315494/command/" -H
"Content-Type: application/json" -H  "Authorization: Bearer "some token"" -H  "X-CSRFToken: lhYJJWLgB53saQaOZXWMSxYSe38
pAghCHFivFYW9TuOpBGFi9ubE2s1OIfplDoGh" -d "{  \"input\": \"mkdir /test_openwisp\",  \"connection\": \"ca39961c-d3ca-4e0a-892f-637d29468007\",  \"type
\": \"custom\"}"

and now it gives:

<!doctype html><html lang="en"><head><title>Server Error (500)</title></head><body><h1>Server Error (500)</h1><p></p></body></html>r

sergorl avatar Jul 08 '22 14:07 sergorl

@sergorl please check the log lines generated right after the request which generates 500 error in /opt/openwisp2/log/openwisp2.log and report here.

nemesifier avatar Jul 08 '22 19:07 nemesifier

/opt/openwisp2/log/openwisp2.log:

File "/opt/openwisp2/env/lib/python3.8/site-packages/django/db/models/base.py", line 1405, in full_clean
    raise ValidationError(errors)
django.core.exceptions.ValidationError: {'input': ['Enter valid JSON.', "'mkdir /test_openwisp' is not of type 'object'"]}

I fixed it by this -d "{ \"input\": {\"command\": \"mkdir /test_openwisp\"}, \"connection\": \"ca39961c-d3ca-4e0a-892f-637d29468007\", \"type\": \"custom\"}" - I found it via chrome developer tools:

curl -X POST "https://openwisp.io/api/v1/controller/device/8cb65d82-dfd8-4a36-94ca-206aec315494/command/" -H "Content-Type: application/json" -H  "Authorization: Bearer "some token"" -H  "X-CSRFToken: lhYJJWLgB53saQaOZXWMSxYSe38pAghCHFivFYW9TuOpBGFi9ubE2s1OIfplDoGh" -d "{  \"input\": {\"command\": \"mkdir /test_openwisp\"},  \"connection\": \"ca39961c-d3ca-4e0a-892f-637d29468007\",  \"type\": \"custom\"}"

P.S.: It seems your swagger /api/v1/docs/ lies:

  1. it generates curl with -H "accept: application/json", but it doesn't work. -H "Content-Type: application/json" works.
  2. also should be -H "Authorization: Bearer "some token"" instead of -H "Authorization: "some token""

sergorl avatar Jul 11 '22 07:07 sergorl