Ejabberd Rest-api - 在 Rest-API 调用中输入无效 JSON
Ejabberd Rest-api - Invalid JSON input on Rest-API call
我正在 windows 10 上设置一个 ejabberd-19.02 服务器,将其配置为通过 rest-api 调用管理命令。我已经按照 ejabberd-doc 中的文档进行操作。我还能够生成 oauth 令牌。
http://localhost:5443/oauth/authorization_token?access_token=0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa&token_type=bearer&expires_in=31536000&scope=ejabberd:admin&state=
但是在调用 rest-api 方法时,使用文档中的 curl 命令:
curl -v -X POST -H "X-Admin: true" -H "Authorization: Bearer 0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa" http://localhost:5443/api/get_roster -d '{"user": "anas", "server": "localhost"}'
我收到的消息是:
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* TCP_NODELAY set
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5443 (#0)
> POST /api/get_roster HTTP/1.1
> Host: localhost:5443
> User-Agent: curl/7.55.1
> Accept: */*
> X-Admin: true
> Authorization: Bearer 0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 7 out of 7 bytes
< HTTP/1.1 400 Bad Request
< Content-Length: 20
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Content-Type, Authorization, X-Admin
<
"Invalid JSON input"* Connection #0 to host localhost left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: anas,/
* Could not resolve host: anas,
* Closing connection 1
curl: (6) Could not resolve host: anas,
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: server:/
* Could not resolve host: server
* Closing connection 2
curl: (6) Could not resolve host: server
curl: (3) [globbing] unmatched close brace/bracket in column 10
ejabberd.log 说:
2019-08-07 13:52:08.201 [debug] <0.841.0>@ejabberd_http:init:151 S: [{[<<"api">>],mod_http_api},{[<<"bosh">>],mod_bosh},{[<<"upload">>],mod_http_upload},{[<<"ws">>],ejabberd_http_ws},{[<<"oauth">>],ejabberd_oauth},{[<<"admin">>],ejabberd_web_admin}]
2019-08-07 13:52:08.202 [info] <0.695.0>@ejabberd_listener:accept:238 (<0.841.0>) Accepted connection 127.0.0.1:59208 -> 127.0.0.1:5443
2019-08-07 13:52:08.203 [debug] <0.841.0>@ejabberd_http:process_header:307 (#Port<0.54>) http query: 'POST' <<"/api/get_roster">>
2019-08-07 13:52:08.203 [debug] <0.841.0>@ejabberd_http:process:394 [<<"api">>,<<"get_roster">>] matches [<<"api">>]
2019-08-07 13:52:08.203 [debug] <0.841.0>@mod_http_api:process:198 Bad Request: {error,{1,invalid_json}}
我试过 ejabberd 版本 19.06 和 19.08。
ejabberd.yml
language: "en"
hosts:
- "localhost"
loglevel: 4
log_rotate_size: 10485760
log_rotate_date: ""
log_rotate_count: 1
log_rate_limit: 100
sql_type: mysql
sql_server: "localhost"
sql_database: "ejabberd"
sql_username: "ejabberd"
sql_password: "#######"
## If you want to specify the port:
sql_port: 3306
default_db: sql
auth_method: sql
certfiles:
- "C:/ProgramData/ejabberd/conf/server.pem"
## - "/etc/letsencrypt/live/localhost/fullchain.pem"
## - "/etc/letsencrypt/live/localhost/privkey.pem"
ca_file: "C:/ProgramData/ejabberd/conf/cacert.pem"
listen:
-
port: 5222
module: ejabberd_c2s
max_stanza_size: 262144
shaper: c2s_shaper
access: c2s
starttls_required: true
-
port: 5269
module: ejabberd_s2s_in
max_stanza_size: 524288
-
port: 5443
module: ejabberd_http
request_handlers:
"/api": mod_http_api
"/bosh": mod_bosh
"/upload": mod_http_upload
"/ws": ejabberd_http_ws
"/oauth": ejabberd_oauth
web_admin: true
captcha: false
tls: false
-
port: 5280
module: ejabberd_http
web_admin: true
-
port: 1883
module: mod_mqtt
backlog: 1000
s2s_use_starttls: optional
acl:
local:
user_regexp: ""
loopback:
ip:
- "127.0.0.0/8"
admin:
user:
- "admin@localhost"
access_rules:
local:
- allow: local
c2s:
- deny: blocked
- allow
announce:
- allow: admin
configure:
- allow: admin
muc_create:
- allow: local
pubsub_createnode:
- allow: local
register:
- allow
trusted_network:
- allow: loopback
api_permissions:
"console commands":
from:
- ejabberd_ctl
who: all
what: "*"
"admin access":
who:
- access:
- allow:
- acl: loopback
- acl: admin
- oauth:
- scope: "ejabberd:admin"
- access:
- allow:
- acl: loopback
- acl: admin
what:
- "*"
- "!stop"
- "!start"
"public commands":
who:
- ip: "127.0.0.1/8"
what:
- "status"
- "connected_users_number"
commands_admin_access:
- allow:
- user: "admin@localhost"
commands:
- add_commands: [user, admin, open]
oauth_expire: 31536000
oauth_access:
- allow:
- user: "admin@localhost"
shaper:
normal: 1000
fast: 50000
shaper_rules:
max_user_sessions: 10
max_user_offline_messages:
- 5000: admin
- 100
c2s_shaper:
- none: admin
- normal
s2s_shaper: fast
max_fsm_queue: 10000
acme:
contact: "mailto:admin@localhost"
ca_url: "https://acme-v01.api.letsencrypt.org"
modules:
mod_adhoc: {}
mod_admin_extra: {}
mod_announce:
access: announce
mod_avatar: {}
mod_blocking: {}
mod_bosh: {}
mod_caps: {}
mod_carboncopy: {}
mod_client_state: {}
mod_configure: {}
mod_disco: {}
mod_fail2ban: {}
mod_http_api: {}
mod_http_upload:
put_url: "https://@HOST@:5443/upload"
mod_last: {}
## mod_mam:
## Mnesia is limited to 2GB, better to use an SQL backend
## For small servers SQLite is a good fit and is very easy
## to configure. Uncomment this when you have SQL configured:
## db_type: sql
## assume_mam_usage: true
## default: always
mod_mqtt: {}
mod_muc:
access:
- allow
access_admin:
- allow: admin
access_create: muc_create
access_persistent: muc_create
default_room_options:
allow_subscription: true # enable MucSub
## mam: true
mod_muc_admin: {}
mod_offline:
access_max_user_messages: max_user_offline_messages
mod_ping: {}
mod_privacy: {}
mod_private: {}
mod_proxy65:
access: local
max_connections: 5
mod_pubsub:
access_createnode: pubsub_createnode
plugins:
- "flat"
- "pep"
force_node_config:
## Change from "whitelist" to "open" to enable OMEMO support
## See https://github.com/processone/ejabberd/issues/2425
"eu.siacs.conversations.axolotl.*":
access_model: whitelist
## Avoid buggy clients to make their bookmarks public
"storage:bookmarks":
access_model: whitelist
mod_push: {}
mod_push_keepalive: {}
mod_register:
## Only accept registration requests from the "trusted"
## network (see access_rules section above).
## Think twice before enabling registration from any
## address. See the Jabber SPAM Manifesto for details:
## https://github.com/ge0rg/jabber-spam-fighting-manifesto
ip_access: trusted_network
mod_roster:
versioning: true
mod_s2s_dialback: {}
mod_shared_roster: {}
mod_stream_mgmt:
resend_on_timeout: if_offline
mod_vcard: {}
mod_vcard_xupdate: {}
mod_version:
show_os: false
您尝试提供 JSON 数据,但如您所见,curl 仅发送 7 个字节的内容。 7个字节只有7个字符,看这个例子:
$ curl -v -X POST -H "X-Admin: true" -H "Authorization: Bearer UsGV4Ap1zHaaNxJNLiYncn9QG5TRXfEW" http://localhost:5443/api/get_roster -d '{"abcde'
.....
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 7 out of 7 bytes
....
所以你用来 运行 curl 的控制台或程序一定有问题,它会切断命令行。在此示例中,如您所见,一个好的 JSON 数据需要 40 个字节(在我的例子中,问题是我没有设置管理员权限):
$ curl -v -X POST -H "X-Admin: true" -H "Authorization: Bearer UsGV4Ap1zHaaNxJNLiYncn9QG5TRXfEW" http://localhost:5443/api/get_roster -d '{"user": "user1", "server": "localhost"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Expire in 1 ms for 1 (transfer 0x557900f65a30)
* Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x557900f65a30)
* Connected to localhost (127.0.0.1) port 5443 (#0)
> POST /api/get_roster HTTP/1.1
> Host: localhost:5443
> User-Agent: curl/7.64.0
> Accept: */*
> X-Admin: true
> Authorization: Bearer UsGV4Ap1zHaaNxJNLiYncn9QG5TRXfEW
> Content-Length: 40
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 40 out of 40 bytes
< HTTP/1.1 403 Forbidden
< Content-Length: 111
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Content-Type, Authorization, X-Admin
<
* Connection #0 to host localhost left intact
{"status":"error","code":32,"message":"AccessRules: Account does not have the right to perform the operation."}
我正在 windows 10 上设置一个 ejabberd-19.02 服务器,将其配置为通过 rest-api 调用管理命令。我已经按照 ejabberd-doc 中的文档进行操作。我还能够生成 oauth 令牌。
http://localhost:5443/oauth/authorization_token?access_token=0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa&token_type=bearer&expires_in=31536000&scope=ejabberd:admin&state=
但是在调用 rest-api 方法时,使用文档中的 curl 命令:
curl -v -X POST -H "X-Admin: true" -H "Authorization: Bearer 0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa" http://localhost:5443/api/get_roster -d '{"user": "anas", "server": "localhost"}'
我收到的消息是:
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* TCP_NODELAY set
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 5443 (#0)
> POST /api/get_roster HTTP/1.1
> Host: localhost:5443
> User-Agent: curl/7.55.1
> Accept: */*
> X-Admin: true
> Authorization: Bearer 0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 7 out of 7 bytes
< HTTP/1.1 400 Bad Request
< Content-Length: 20
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Content-Type, Authorization, X-Admin
<
"Invalid JSON input"* Connection #0 to host localhost left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: anas,/
* Could not resolve host: anas,
* Closing connection 1
curl: (6) Could not resolve host: anas,
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: server:/
* Could not resolve host: server
* Closing connection 2
curl: (6) Could not resolve host: server
curl: (3) [globbing] unmatched close brace/bracket in column 10
ejabberd.log 说:
2019-08-07 13:52:08.201 [debug] <0.841.0>@ejabberd_http:init:151 S: [{[<<"api">>],mod_http_api},{[<<"bosh">>],mod_bosh},{[<<"upload">>],mod_http_upload},{[<<"ws">>],ejabberd_http_ws},{[<<"oauth">>],ejabberd_oauth},{[<<"admin">>],ejabberd_web_admin}] 2019-08-07 13:52:08.202 [info] <0.695.0>@ejabberd_listener:accept:238 (<0.841.0>) Accepted connection 127.0.0.1:59208 -> 127.0.0.1:5443 2019-08-07 13:52:08.203 [debug] <0.841.0>@ejabberd_http:process_header:307 (#Port<0.54>) http query: 'POST' <<"/api/get_roster">> 2019-08-07 13:52:08.203 [debug] <0.841.0>@ejabberd_http:process:394 [<<"api">>,<<"get_roster">>] matches [<<"api">>] 2019-08-07 13:52:08.203 [debug] <0.841.0>@mod_http_api:process:198 Bad Request: {error,{1,invalid_json}}
我试过 ejabberd 版本 19.06 和 19.08。
ejabberd.yml
language: "en"
hosts:
- "localhost"
loglevel: 4
log_rotate_size: 10485760
log_rotate_date: ""
log_rotate_count: 1
log_rate_limit: 100
sql_type: mysql
sql_server: "localhost"
sql_database: "ejabberd"
sql_username: "ejabberd"
sql_password: "#######"
## If you want to specify the port:
sql_port: 3306
default_db: sql
auth_method: sql
certfiles:
- "C:/ProgramData/ejabberd/conf/server.pem"
## - "/etc/letsencrypt/live/localhost/fullchain.pem"
## - "/etc/letsencrypt/live/localhost/privkey.pem"
ca_file: "C:/ProgramData/ejabberd/conf/cacert.pem"
listen:
-
port: 5222
module: ejabberd_c2s
max_stanza_size: 262144
shaper: c2s_shaper
access: c2s
starttls_required: true
-
port: 5269
module: ejabberd_s2s_in
max_stanza_size: 524288
-
port: 5443
module: ejabberd_http
request_handlers:
"/api": mod_http_api
"/bosh": mod_bosh
"/upload": mod_http_upload
"/ws": ejabberd_http_ws
"/oauth": ejabberd_oauth
web_admin: true
captcha: false
tls: false
-
port: 5280
module: ejabberd_http
web_admin: true
-
port: 1883
module: mod_mqtt
backlog: 1000
s2s_use_starttls: optional
acl:
local:
user_regexp: ""
loopback:
ip:
- "127.0.0.0/8"
admin:
user:
- "admin@localhost"
access_rules:
local:
- allow: local
c2s:
- deny: blocked
- allow
announce:
- allow: admin
configure:
- allow: admin
muc_create:
- allow: local
pubsub_createnode:
- allow: local
register:
- allow
trusted_network:
- allow: loopback
api_permissions:
"console commands":
from:
- ejabberd_ctl
who: all
what: "*"
"admin access":
who:
- access:
- allow:
- acl: loopback
- acl: admin
- oauth:
- scope: "ejabberd:admin"
- access:
- allow:
- acl: loopback
- acl: admin
what:
- "*"
- "!stop"
- "!start"
"public commands":
who:
- ip: "127.0.0.1/8"
what:
- "status"
- "connected_users_number"
commands_admin_access:
- allow:
- user: "admin@localhost"
commands:
- add_commands: [user, admin, open]
oauth_expire: 31536000
oauth_access:
- allow:
- user: "admin@localhost"
shaper:
normal: 1000
fast: 50000
shaper_rules:
max_user_sessions: 10
max_user_offline_messages:
- 5000: admin
- 100
c2s_shaper:
- none: admin
- normal
s2s_shaper: fast
max_fsm_queue: 10000
acme:
contact: "mailto:admin@localhost"
ca_url: "https://acme-v01.api.letsencrypt.org"
modules:
mod_adhoc: {}
mod_admin_extra: {}
mod_announce:
access: announce
mod_avatar: {}
mod_blocking: {}
mod_bosh: {}
mod_caps: {}
mod_carboncopy: {}
mod_client_state: {}
mod_configure: {}
mod_disco: {}
mod_fail2ban: {}
mod_http_api: {}
mod_http_upload:
put_url: "https://@HOST@:5443/upload"
mod_last: {}
## mod_mam:
## Mnesia is limited to 2GB, better to use an SQL backend
## For small servers SQLite is a good fit and is very easy
## to configure. Uncomment this when you have SQL configured:
## db_type: sql
## assume_mam_usage: true
## default: always
mod_mqtt: {}
mod_muc:
access:
- allow
access_admin:
- allow: admin
access_create: muc_create
access_persistent: muc_create
default_room_options:
allow_subscription: true # enable MucSub
## mam: true
mod_muc_admin: {}
mod_offline:
access_max_user_messages: max_user_offline_messages
mod_ping: {}
mod_privacy: {}
mod_private: {}
mod_proxy65:
access: local
max_connections: 5
mod_pubsub:
access_createnode: pubsub_createnode
plugins:
- "flat"
- "pep"
force_node_config:
## Change from "whitelist" to "open" to enable OMEMO support
## See https://github.com/processone/ejabberd/issues/2425
"eu.siacs.conversations.axolotl.*":
access_model: whitelist
## Avoid buggy clients to make their bookmarks public
"storage:bookmarks":
access_model: whitelist
mod_push: {}
mod_push_keepalive: {}
mod_register:
## Only accept registration requests from the "trusted"
## network (see access_rules section above).
## Think twice before enabling registration from any
## address. See the Jabber SPAM Manifesto for details:
## https://github.com/ge0rg/jabber-spam-fighting-manifesto
ip_access: trusted_network
mod_roster:
versioning: true
mod_s2s_dialback: {}
mod_shared_roster: {}
mod_stream_mgmt:
resend_on_timeout: if_offline
mod_vcard: {}
mod_vcard_xupdate: {}
mod_version:
show_os: false
您尝试提供 JSON 数据,但如您所见,curl 仅发送 7 个字节的内容。 7个字节只有7个字符,看这个例子:
$ curl -v -X POST -H "X-Admin: true" -H "Authorization: Bearer UsGV4Ap1zHaaNxJNLiYncn9QG5TRXfEW" http://localhost:5443/api/get_roster -d '{"abcde'
.....
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 7 out of 7 bytes
....
所以你用来 运行 curl 的控制台或程序一定有问题,它会切断命令行。在此示例中,如您所见,一个好的 JSON 数据需要 40 个字节(在我的例子中,问题是我没有设置管理员权限):
$ curl -v -X POST -H "X-Admin: true" -H "Authorization: Bearer UsGV4Ap1zHaaNxJNLiYncn9QG5TRXfEW" http://localhost:5443/api/get_roster -d '{"user": "user1", "server": "localhost"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Expire in 1 ms for 1 (transfer 0x557900f65a30)
* Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x557900f65a30)
* Connected to localhost (127.0.0.1) port 5443 (#0)
> POST /api/get_roster HTTP/1.1
> Host: localhost:5443
> User-Agent: curl/7.64.0
> Accept: */*
> X-Admin: true
> Authorization: Bearer UsGV4Ap1zHaaNxJNLiYncn9QG5TRXfEW
> Content-Length: 40
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 40 out of 40 bytes
< HTTP/1.1 403 Forbidden
< Content-Length: 111
< Content-Type: application/json
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Headers: Content-Type, Authorization, X-Admin
<
* Connection #0 to host localhost left intact
{"status":"error","code":32,"message":"AccessRules: Account does not have the right to perform the operation."}