Ansible的uri模块请求返回401但shell执行curl正常,求正确uri写法
问题根因
你原有uri模块写法的核心错误是body_format参数配置不匹配:
- curl命令发送的是
application/x-www-form-urlencoded格式的表单请求体 - 但你的uri配置里把
body_format设为了json,会把请求体序列化为JSON格式,后端无法正常解析账号密码参数,因此返回401认证错误。
正确uri模块实现
- name: Get authentication token uri: url: https://myIPcontrol.com:8443/inc-rest/api/v1/login method: POST timeout: 30 validate_certs: no headers: Content-Type: application/x-www-form-urlencoded Accept: application/json body: username: admin password: mypsw body_format: form-urlencoded return_content: yes register: authtoken
当body_format设置为form-urlencoded时,Ansible会自动把body字段的字典内容转换为key1=value1&key2=value2的表单格式,和你curl命令的发送效果完全一致。
内容的提问来源于stack exchange,提问作者sundrys1
相关产品推荐
相关产品推荐

