Run API requests from the command line.
- Define the requests in a file
- Capture values from the response
- Chain requests to implement full API use cases
- Own your files, no cloud storage
% nugget requests.nUse the --raw flag to print the raw response.
% nugget requests.n --rawPass the -q flag to print just the response body.
% nugget requests.n -qThis would be a simple nugget file with a get request:
# Get TODO list
GET http://mytodo.com/api/v1/todos
This would print a text before the output of the request (to label the output):
GET http://mytodo.com/api/v1/todos
ECHO Get TODO list
Use the skip keyword to skip a request:
# This will run
GET http://mytodo.com/api/v1/todos
# This will not
GET http://mytodo.com/api/v1/todos/123
SKIP
You can add additional headers. nugget already takes care of the Content-type and Authorization headers (it uses Bearer Authentication).
GET http://mytodo.com/api/v1/todos
header some-header some-value
Use the http keyword to assert the status code of the response.
GET http://mytodo.com/api/v1/todos
HTTP 200
Use the SAVE keyword to save one or more values from the response:
# Create TODO item
POST https://mytodos.com/api/v1/todos/create
{
"name": "Go shopping",
"due": "2024-06-09"
}
SAVE todo_id .id
SAVE todo_name .name
To save values, use the path of the value you want to save from the response. For example, .address.country if you need to save 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 saved values adding the variable name in a "template" like fashion: {{ .variable-name }}.
The saved values can be use in the following areas:
- The body json
- The url
- The header
To chain several requests, just add more requests to the same nugget file. Use the saved values as explained before.
# Create TODO item
POST https://mytodos.com/api/v1/todos/create
{
"name": "Go shopping",
"due": "2024-06-09"
}
SAVE todo_id: .id
WAIT 1000
# Update the previous TODO
PUT https://mytodos.com/api/v1/todos/{{ .todo_id }}/update
{
"name": "Go grocery shopping"
}
# Stop until ENTER is pressed
WAIT -1
# Update the previous TODO again
PUT https://mytodos.com/api/v1/todos/{{ .todo_id }}/update
{
"name": "Go shopping"
}
nugget has the following keywords:
#: comments (full line comments supported only)GET, POST, PUT, DELETE, PATCH: type of requestHTTP: http response code assertHEADER: add request headerSAVE: save a value from the response to a variableECHO: print the provided text before the request outputSKIP: skip the requestWAIT: wait for a certain amount of milliseconds before the next request; use-1to continue afterENTERis pressed
And the following pre-defined template variables:
{{ .uuid }}: generates a random UUID
Add this code to your .emacs file:
(require 'generic-x)
(define-generic-mode 'nugget-mode
'("#")
'("POST" "GET" "PUT" "DELETE" "PATCH"
"HTTP" "HEADER" "SAVE" "ECHO" "SKIP" "WAIT"
"post" "get" "put" "delete" "patch"
"http" "header" "save" "echo" "skip" "wait")
'(("{{[[:space:]]*\\(\\.[a-zA-Z_][a-zA-Z0-9_-]*\\)[[:space:]]*}}"
0 font-lock-preprocessor-face)
("\\_<[-+]?[0-9]*\\.?[0-9]+\\_>" . font-lock-constant-face))
'("\\.n\\'")
nil
"Nugget syntax highlighting."
)
(add-to-list 'auto-mode-alist '("\\.n\\'" . nugget-mode))Isaac Benitez
June 2024