0

我想通过它的 REST api与 seafile 服务器交互。

到目前为止,我将 POST 或 GET 查询转换为 ansible uri 模块没有问题。但是,我无法让 PUT 查询正常工作。

以下适用于 curl:

curl -X PUT -d "share_type=group&group_id=<groupid>&permission=rw" -H 'Authorization: Token <mysecrettoken>' -H 'Accept: application/json; charset=utf-8; indent=4' https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/

当我将其转换为以下 ansible 任务时,它失败了:

- name: mytask
  uri:
    url: "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/"
    method: PUT
    headers: '{ "Authorization": "Token <mysecrettoken>" }'
    body: '{ "share_type": "group", "group_id": "<groupid>", "permission": "rw"}'
    body_format: json
    return_content: yes

我得到错误:

HTTP Error 500: INTERNAL SERVER ERROR", "redirected": false, "server": "nginx", "set_cookie": "SERVERID=<serverid>; path=/", "status": 500, "transfer_encoding": "chunked", "url": "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/", "vary": "Accept-Language, Cookie"}

在使用 requests 库的 python 脚本中,我必须提供最终?p=/params={'p': '/'}. 这是失败的原因吗?那我该如何正确提交参数呢?

4

1 回答 1

1

您应该将 传递headers为 YAML 哈希,而不是 JSON 字符串:

- name: mytask
  uri:
    url: "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/"
    method: PUT
    headers: 
      Authorization: "Token <mysecrettoken>"
    body: '{ "share_type": "group", "group_id": "<groupid>", "permission": "rw"}'
    body_format: json
    return_content: yes

作为参考,请参阅文档,尤其是倒数第二个示例:

- uri:
    url: https://your.form.based.auth.example.com/dashboard.php
    method: GET
    return_content: yes
    headers:
      Cookie: "{{ login.set_cookie }}"
于 2018-10-01T21:47:30.807 回答