首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ejabberd Rest-api -- Rest-API调用上的无效JSON输入

Ejabberd Rest-api -- Rest-API调用上的无效JSON输入
EN

Stack Overflow用户
提问于 2019-08-07 08:31:23
回答 1查看 693关注 0票数 0

我正在windows 10上设置一个ejabberd-19.02服务器,将其配置为通过rest调用管理命令。我遵循了ejabberd中的文档。我还可以生成oauth令牌。

代码语言:javascript
复制
    http://localhost:5443/oauth/authorization_token?access_token=0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa&token_type=bearer&expires_in=31536000&scope=ejabberd:admin&state=

但是在调用rest方法时,使用curl命令,如文档中所示:

代码语言:javascript
复制
   curl -v -X POST -H "X-Admin: true" -H "Authorization: Bearer 0GXCPVJa0l3jFH3Yrn9dIZFMftrNTBFa" http://localhost:5443/api/get_roster -d '{"user": "anas", "server": "localhost"}'

我收到的信息是:

代码语言:javascript
复制
    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调试<0.841.0>@ejabberd_http:init:151 S:[{<<"api">>,mod_http_api},{<<"bosh">>,mod_bosh},{<<“上载”“>>,mod_http_upload}”,{<<"ws">>,ejabberd_http_ws},{<<"oauth">>,ejabberd_oauth},{<<"admin">>,ejabberd_oauth},ejabberd_web_admin}] 2019-08-07 13:52:08.202 info <0.695.0>@ejabberd_listener:accept:238 (<0.841.0>)接受连接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查询:'POST‘<> 2019-08-07 13:52:08.203调试<0.841.0>@ejabberd_http:process:394 <<"api">>,<<"get_roster">>匹配<<"api">> 2019-08-07 13:52:08.203调试<0.841.0>@mod_http_api:process:198坏请求:{error,{1,invalid_json}

我试过ejabberd版本19.06和19.08。

ejabberd.yml

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-07 20:52:43

您试图提供JSON数据,但如您所见,curl只发送7字节的内容。7个字节仅为7个字符,请参见下面的示例:

代码语言:javascript
复制
$ 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个字节(在我的例子中,问题是我没有设置管理权限):

代码语言:javascript
复制
$ 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."}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57390122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档