Run API requests from the command line.
- Define the requests in a yaml file
- Capture values from the response
- Chain requests to implement full API use cases
- Own your files, no cloud storage
% nugget requests.yamlUse the --raw flag to print the raw response.
% nugget requests.yaml --rawPass the -H flag to include the response headers in the output.
% nugget requests.yaml -HThis would be a simple yaml file with a get request:
- name: Get TODO list
method: GET
url: http://mytodo.com/api/v1/todosYou can add additional headers. nugget already takes care of the Content-type and Authorization headers (it uses Bearer Authentication).
- name: Get TODO list
method: GET
url: http://mytodo.com/api/v1/todos
header:
some-header: some-valueUse the http keyword to assert the status code of the response.
- name: Get TODO list
method: GET
url: http://mytodo.com/api/v1/todos
http: 200Use the capture keyword to capture a list of values from the response:
- name: Create TODO item
method: POST
url: https://mytodos.com/api/v1/todos/create
body: |
{
"name": "Go shopping",
"due": "2024-06-09"
}
capture:
todo_id: .id
todo_name: .nameTo capture values, use the path of the value you want to capture from the response. For example, .address.country if you need to capture the country in the following json:
{
"id": 1234,
"name": "John Doe",
"address": {
"street": "333 Embarcadero",
"city": "San Francisco",
"state": "CA",
"country": "US"
}
}You can use the captured values adding the variable name in a "template" like fashion: {{ .variable-name }}.
The captured values can be use in the following areas:
- The body json
- The url
- The header, wrapping the template variable in quoutes:
"{{ .some-header }}"
To chain several requests, just add more steps in the yaml file starting with the name of the step. Use the captured values as explained before.
- name: Create TODO item
method: POST
url: https://mytodos.com/api/v1/todos/create
body: |
{
"name": "Go shopping",
"due": "2024-06-09"
}
capture:
todo_id: .id
wait: 1000
- name: Update the previous TODO
method: PUT
url: https://mytodos.com/api/v1/todos/{{ .todo_id }}/update
body: |
{
"name": "Go grocery shopping"
} nugget has the following keywords:
name: name of the requestmethod: type of requests (GET, POST, PUT, DELETE)url: the endpoint urlhttp: http response code assertheader: list of headersbody: body of the requestcapture: list of variables to capture from the responsewait: wait for a certain amount of milliseconds before the next request
And the following pre-defined template variables:
{{ .uuid }}: generates a random UUID
Isaac Benitez
June 2024