简体   繁体   English

将curl PUT命令转换为ansuri uri

[英]translate a curl PUT command into ansible uri

I want to interact with a seafile server with its REST api . 我想使用其REST api与seafile服务器进行交互。

So far I had no problems translating POST or GET queries into the ansible uri module. 到目前为止,将POST或GET查询转换为ansuri uri模块没有问题。 However, I have a problem with getting a PUT query to work. 但是,我有一个使PUT查询工作的问题。

The following works with curl: 以下适用于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=/

When I translate this to the following ansible task, it fails: 当我将其翻译为以下任务时,它将失败:

- 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

I get the error: 我得到错误:

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"}

In a python script using the requests library, I had to supply the final ?p=/ as params={'p': '/'} . 在使用请求库的python脚本中,我必须提供最终的?p=/作为params={'p': '/'} Is this the reason for the failure? 这是失败的原因吗? How do I correctly submit the parameter then? 那我该如何正确提交参数呢?

You should pass the headers as a YAML hash, not as a JSON string: 您应该将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

For reference, see the docs , especially the second-to-last example: 作为参考,请参阅docs ,尤其是倒数第二个示例:

- uri:
    url: https://your.form.based.auth.example.com/dashboard.php
    method: GET
    return_content: yes
    headers:
      Cookie: "{{ login.set_cookie }}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM