diff --git a/docs/docs/usage/automatic-response-formatting.md b/docs/docs/usage/automatic-response-formatting.md new file mode 100644 index 0000000..32fd6c7 --- /dev/null +++ b/docs/docs/usage/automatic-response-formatting.md @@ -0,0 +1,39 @@ +# Automatic Response Formatting + +If you want to automatically format the response of an HTTP request, +you can use simply add an `accept` header with the desired format. + +For example, if you want to receive the response in JSON format, you can add the `accept: application/json` header. + +```http title="automatic-response-formatting.http" +POST https://httpbin.org/post HTTP/1.1 +content-type: application/json +accept: application/json + +{ + "uuid": "{{$uuid}}", + "timestamp": "{{$timestamp}}", + "date": "{{$date}}", + "randomInt": "{{$randomInt}}", +} + +``` + +**NOTE:** You need to have external tools to format the response. +For example, `jq` for JSON, `xmllint` for XML and HTML, etc. + +### Supported Formats + +- JSON: `application/json` +- XML: `application/xml` +- HTML: `text/html` + +### Default formatters + +```lua title="default-formatters.lua" +formatters = { + json = { "jq", "." }, + xml = { "xmllint", "--format", "-" }, + html = { "xmllint", "--format", "--html", "-" }, +} +``` diff --git a/docs/docs/usage/magic-variables.md b/docs/docs/usage/magic-variables.md new file mode 100644 index 0000000..0c0ea09 --- /dev/null +++ b/docs/docs/usage/magic-variables.md @@ -0,0 +1,28 @@ +# Magic Variables + +There is a predefined set of magic variables that you can use in your HTTP requests. + +They all start with a `$` sign. + +- `{{$uuid}}` - Generates a random UUID. +- `{{$timestamp}}` - Generates a timestamp. +- `{{$date}}` - Generates a date (YYYY-MM-DD). +- `{{$randomInt}}` - Generates a random integer (between 0 and 9999999). + +To test this feature, create a file with the `.http` extension and write your HTTP requests in it. + +```http title="magic-variables.http" + +POST https://httpbin.org/post HTTP/1.1 +content-type: application/json +accept: application/json + +{ + "uuid": "{{$uuid}}", + "timestamp": "{{$timestamp}}", + "date": "{{$date}}", + "randomInt": "{{$randomInt}}", +} + +### +``` diff --git a/lua/kulala/parser/dynamic_vars.lua b/lua/kulala/parser/dynamic_vars.lua index 5df1f02..73bb35c 100644 --- a/lua/kulala/parser/dynamic_vars.lua +++ b/lua/kulala/parser/dynamic_vars.lua @@ -1,7 +1,4 @@ -local FS = require("kulala.utils.fs") -local GLOBALS = require("kulala.globals") local CONFIG = require("kulala.config") -local STRING_UTILS = require("kulala.utils.string") local M = {} local random = math.random @@ -18,14 +15,6 @@ local function uuid() end) end -local previous_response_body = function() - local previous_response = FS.read_file(GLOBALS.BODY_FILE) - if not previous_response then - return "" - end - return STRING_UTILS.trim(previous_response) -end - ---Retrieve all dynamic variables from both rest.nvim and the ones declared by ---the user on his configuration ---@return { [string]: fun():string }[] An array-like table of tables which contains dynamic variables definition @@ -37,9 +26,8 @@ function M.retrieve_all() return os.date("%Y-%m-%d") end, ["$timestamp"] = os.time, - ["$previousResponseBody"] = previous_response_body, ["$randomInt"] = function() - return math.random(0, 1000) + return math.random(0, 9999999) end, }