From f48f5c06d91734bfebf7fd62252444b06a55e2ca Mon Sep 17 00:00:00 2001 From: Martin Rode Date: Thu, 9 Nov 2023 16:04:39 +0100 Subject: [PATCH] New funcs remove_from_url & value_from_url --- README.md | 8 ++++++++ pkg/lib/template/template_loader.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/README.md b/README.md index ed10699..84b989e 100644 --- a/README.md +++ b/README.md @@ -2276,6 +2276,14 @@ Example: Write **msg** to log output. Args can be given. This uses logrus.Debugf to output. +## `remove_from_url` [key] [url] + +Removes from **key** from **url**'s query, returns the **url** with the **key** removed. In case of an error, the **url** is returned as is. Unparsable urls are ignored and the **url** is returned. + +## `value_from_url` [key] + +Returns the **value** from the **url**'s query for **key**. In case of an error, an empty string is returned. Unparsable urls are ignored and an empty string is returned. + # HTTP Server The apitest tool includes an HTTP Server. It can be used to serve files from the local disk temporarily. The HTTP Server can run in test mode. In this mode, the apitest tool does not run any tests, but starts the HTTP Server in the foreground, until CTRL-C in pressed. diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index a73c6b1..b67f4b8 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -455,6 +455,27 @@ func (loader *Loader) Render( logrus.Debugf(msg, args...) return "" }, + // remove_from_url removes key from url's query part, returns + // the new url + "remove_from_url": func(qKey, urlStr string) (urlPatched string) { + u, err := url.Parse(urlStr) + if err != nil { + return urlStr + } + q := u.Query() + q.Del(qKey) + u.RawQuery = q.Encode() + return u.String() + }, + // value_from_url returns the value from url's query part + "value_from_url": func(qKey, urlStr string) string { + u, err := url.Parse(urlStr) + if err != nil { + return "" + } + q := u.Query() + return q.Get(qKey) + }, } tmpl, err := template. New("tmpl").