why URI module ignores body. I need to send body to get the response what I need. Python request module works fine.
---
- name: Get info
hosts: local_host
gather_facts: no
vars:
auth_key: 'xxxxxx'
tasks:
- name: Fetch all entries matching
uri:
url: "https://get_changes.com?"
method: GET
headers:
Content-Type: "application/json"
Accept: "application/json"
Authorization: 'Basic {{auth_key}}'
Timeout: '30'
body_format: json
body:
sysparm_fields: "number, state"
sysparm_query:"cur_state=10^number=abcdef^u_ci_infra_type=network^name=Network"
return_content: yes
status_code: 200
register: crb_output
Ah I see. That's true. Args should be in URL itself for GET. forgot about that. Thanks.
Related
I am using Ansible's URI module to call a simple GET endpoint but I am getting a 401 error message as shown below.
Here is the playbook configuration
- name: Elasticsearch Cluster health Check
uri:
url: https://elastichost:9200/_cluster/health?&pretty
url_password : xxxxxxx
url_username : xxxxxx
validate_certs : no
method: GET
body_format: json
force_basic_auth: yes
return_content: yes
headers:
Content-Type: "application/json"
register: showhealth
tags: NonProd
- name: Show Elasticsearch Health
debug: var=showhealth
tags: NonProd
Any inputs on how to resolve
specific issue with elastic. elastic is secure, you need token or certificate to connect to api.
And ofc, your's user need correct right.
exemple
- name: Create update_user
uri:
url: 'uri'
method: POST
user: elasticuser
body_format: json
headers:
Content-Type: application/json
body: '{{ item.body }}'
client_cert: 'certificate.crt'
client_key: 'certificate.key'
password: '{{ elastic_password }}'
validate_certs: false
return_content: true
I trying to use taurus and test my restapi.
My restapi using JSON body in POST requests.
So i can't find anywhere how to put json into my POST request.
I tryed like this, but this not work.
execution:
- concurrency: 25
throughput: 25
ramp-up: 1m
hold-for: 5m
steps: 3
scenario: blazemeter-recording
scenarios:
blazemeter-recording:
timeout: 5s
retrieve-resources: false
store-cache: true
store-cookie: false
default-address: https://someurl
headers:
User-Agent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
Accept-Language: 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4'
Accept-Encoding: 'gzip, deflate, sdch'
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp'
requests:
- url: '/api/v8/url/url/url'
method: POST
label: '/api/v8/url/url/url'
headers:
Content-Type: application/json
body:
"{\"applicationId\":1,\"objectId\":196,\"tags\":[{\"tag\":\"#ObjectsFilter:filter:Data#\",\"objectId\":196,\"pagination\":{\"pageSize\":100}}]}"
I found solution, i can use "body-file"
- url: 'url'
method: POST
label: 'data label'
headers:
Content-Type: application/json
body-file: /var/tests/json_event_filter.json
Your configuration is just fine, looking into Taurus JMeter Executor HTTP Requests documentation
body: 'request-body-string' # if present, will be used as body
You can double check it by running Taurus in GUI mode like:
bzt your-test.yaml -gui
or
bzt -o modules.jmeter.gui=true your-test.yaml
You should see that HTTP request sampler has your JSON payload in the "Body Data" tab:
More information: Navigating your First Steps Using Taurus
What I'm trying to do
Everything concern HTTP requests.
Create something using POST API, if created I can get the resource id
If resource already created, it returns 422 status code
If 422 status code, get resource by name so I can access it's id
Create another resource with the given id inside its request body
The code I have
- name: Create cluster
uri:
url: '{{ rancher_url }}/v3/cluster'
method: POST
body: {"type":"cluster","nodes":[],"rancherKubernetesEngineConfig":{"ignoreDockerVersion":true, "network":{"type":"networkConfig","plugin":"{{ rancher_network_provider }}"}},"name":"{{ rancher_cluster_name }}"}
status_code: 201, 422
body_format: json
headers:
Authorization: "Bearer {{ api_key_result.json.token }}"
validate_certs: "{{ validate_certs }}"
register: cluster_created
- name: Cluster already exists, retrieving id
uri:
url: '{{ rancher_url }}/v3/cluster?name={{ rancher_cluster_name }}'
method: GET
status_code: 200
headers:
Authorization: "Bearer {{ api_key_result.json.token }}"
body_format: json
validate_certs: "{{ validate_certs }}"
register: cluster_found
when: cluster_created.status == 422
- name: Get command to launch for nodes
uri:
url: '{{ rancher_url }}/v3/clusterregistrationtoken'
method: POST
body: {"type":"clusterRegistrationToken","clusterId":"{{cluster_created.json.id | cluster_found.json.data[0].id}}"}
status_code: 201
body_format: json
headers:
Authorization: "Bearer {{ api_key_result.json.token }}"
validate_certs: "{{ validate_certs }}"
register: node_command_result
So basically I want this line to work : {{cluster_created.json.id | cluster_found.json.data[0].id}}
The error I get
TASK [rancher_worker : Get command to launch for nodes] ***************************************************************************************************************************************************************************************
fatal: [51.15.25.38]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got '['. String: {{cluster_created.json.id | cluster_found.json.data[0].id}}"}
What do you think ?
Thanks for your help :)
In this instance I'm using the uri module to smoketest an API deployment. The flow is:
Attempt to log test user in and capture JWT token
If #1 fails register new user
If #2 fired simulate clicking the verify user email link
If #3 fired log the new test user in and capture JWT token
In #1 or #4 I'd like to assign the JWT from the result to a variable for later use. Something like:
- name: log user in
uri:
url: "{{ api_url }}/user"
method: post
body_format: json
body:
email: "{{ email }}"
password: "{{ password }}"
assign:
jwt: {{ response.json.data.jwt }}
As if assign were a thing. I could register: session and use {{ session.json.data.jwt}} a hundred times in the rest of the smoke tests without it but it would be so much cleaner if I could mutate a play scoped variable, perhaps defined at the top of the playlist.
The answer came from #matt-p in the comments - use set_fact. Thanks.
I wanted to show off how well this works on a REST API. Notice how we can later access keys from the returned JSON with a simple {{ user.email }} and use the JWT token with headers:
jwt={{ user.jwt }}
- name: Login and get JWT session
uri:
url: "{{ url }}/users/login"
body_format: json
body:
email: "{{ email }}"
password: "{{ password }}"
method: POST
status_code: [200]
register: user_login
- name: Assign user variable to user_login results
set_fact:
user: "{{ user_login.json.data }}"
- name: Create a post
uri:
url: "{{ url }}/users/{{ user.id }}/posts"
body_format: json
method: POST
headers: jwt={{ user.jwt }}
body:
content: >
My name is {{ user.firstName }} and you
can email me at {{ user.email }}
status_code: [201]
register: post_creation
I want to interact with a seafile server with its REST api.
So far I had no problems translating POST or GET queries into the ansible uri module. However, I have a problem with getting a PUT query to work.
The following works with 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': '/'}. 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:
- 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:
- uri:
url: https://your.form.based.auth.example.com/dashboard.php
method: GET
return_content: yes
headers:
Cookie: "{{ login.set_cookie }}"