Bootstrap FreeKB - Ansible - Submit a POST request to a REST API using the uri module
Ansible - Submit a POST request to a REST API using the uri module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The uri module can be used to submit a POST request to a REST API.

Sometimes the REST API will return a response code that Ansible thinks indicates that the POST request failed, when in fact the POST request was successful. For example, I had an REST API that was returning a status code of 204 when the reqeust was successful, and Ansible interpreted this as a failure. To account for this, I added failed_when so that the task was not failed when the response code was 204.

  • If the REST API requires basic authentication, the force_basic_authurl_username and url_password options can be included.
  • If the POST request is used by appended key value pairs to the URL, meaning that the “Content-Type: application/x-www-form-urlencoded” header is used, this header is set using body_format: form-urlencoded.
---
- hosts: all
  tasks:
  - name: submit POST request
    uri:
      url: http://example.com/api/v1?foo=hello&bar=world
      method: POST
      body_format: form-urlencoded
      force_basic_auth: yes
      url_username: john.doe
      url_password: itsasecret
      validate_certs: no
    register: out
    failed_when: out.status != 204

  - debug:
      var: out
...

 

And here is an example with an oAuth bearer token passed in as a header.

---
- hosts: all
  tasks:
  - name: submit POST request
    uri:
      url: http://example.com/api/v1
      method: POST
      headers:
        Authorization: "Bearer abc123"
        Accept: application/json
        Content-Type: application/json
    register: out
    failed_when: out.status != 204

  - debug:
      var: out
...

 

If the POST request is successful, something like this should be returned.

ok: [server1.example.com] => {
    "out": {
        "msg": "[200]: OK", 
        "pragma": "no-cache", 
        "redirected": false, 
        "server": "******", 
        "set_cookie": "JSESSIONID=7B01203DD8CA499717F5BD3B36A6D929; Path=/example; HttpOnly; SameSite=Strict", 
        "status": 200, 
        "url": "http://example.com/api/v1?foo=hello&bar=world", 
        "x_content_type_options": "NOSNIFF", 
        "x_frame_options": "SAMEORIGIN", 
        "x_xss_protection": "1; mode=block"
     }

 

If you will be passing in unqiue URL string, it's probably a good idea to use set_fact and urlencode.

---
- hosts: all
  tasks:
  - set_fact:
      my_url: input_string | urlencode

  - name: submit POST request
    uri:
      url: "{{ my_url }}"
      method: POST
      body_format: form-urlencoded
      force_basic_auth: yes
      url_username: john.doe
      url_password: itsasecret
      validate_certs: no
    register: out
    failed_when: out.status != 204

  - debug:
      var: out
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 7ea97a in the box below so that we can be sure you are a human.