From 261f0f52618e7727ed6b3e1a3dc872e79e3fe1ba Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Fri, 13 Jan 2023 11:34:25 +0000 Subject: [PATCH] Add query string support for tm-http-request --- core/modules/utils/dom/http.js | 25 +++++++++++++++++++ .../WidgetMessage_ tm-http-request.tid | 4 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/core/modules/utils/dom/http.js b/core/modules/utils/dom/http.js index 6b9ef4bb2..8a1fbb27f 100644 --- a/core/modules/utils/dom/http.js +++ b/core/modules/utils/dom/http.js @@ -32,7 +32,9 @@ HttpClient.prototype.handleHttpRequest = function(event) { bindProgress = paramObject["bind-progress"], method = paramObject.method || "GET", HEADER_PARAMETER_PREFIX = "header-", + QUERY_PARAMETER_PREFIX = "query-", PASSWORD_HEADER_PARAMETER_PREFIX = "password-header-", + PASSWORD_QUERY_PARAMETER_PREFIX = "password-query-", CONTEXT_VARIABLE_PARAMETER_PREFIX = "var-", requestHeaders = {}, contextVariables = {}, @@ -45,11 +47,19 @@ HttpClient.prototype.handleHttpRequest = function(event) { setBinding(bindStatus,"pending"); setBinding(bindProgress,"0"); $tw.utils.each(paramObject,function(value,name) { + // Look for query- parameters + if(name.substr(0,QUERY_PARAMETER_PREFIX.length) === QUERY_PARAMETER_PREFIX) { + url = $tw.utils.setQueryStringParameter(url,name.substr(QUERY_PARAMETER_PREFIX.length),value); + } // Look for header- parameters if(name.substr(0,HEADER_PARAMETER_PREFIX.length) === HEADER_PARAMETER_PREFIX) { requestHeaders[name.substr(HEADER_PARAMETER_PREFIX.length)] = value; } // Look for password-header- parameters + if(name.substr(0,PASSWORD_QUERY_PARAMETER_PREFIX.length) === PASSWORD_QUERY_PARAMETER_PREFIX) { + url = $tw.utils.setQueryStringParameter(url,name.substr(PASSWORD_QUERY_PARAMETER_PREFIX.length),$tw.utils.getPassword(value) || ""); + } + // Look for password-query- parameters if(name.substr(0,PASSWORD_HEADER_PARAMETER_PREFIX.length) === PASSWORD_HEADER_PARAMETER_PREFIX) { requestHeaders[name.substr(PASSWORD_HEADER_PARAMETER_PREFIX.length)] = $tw.utils.getPassword(value) || ""; } @@ -201,4 +211,19 @@ exports.httpRequest = function(options) { return request; }; +exports.setQueryStringParameter = function(url,paramName,paramValue) { + var URL = $tw.browser ? window.URL : require("url").URL, + newUrl; + try { + newUrl = new URL(url); + } catch(e) { + } + if(newUrl && paramName) { + newUrl.searchParams.set(paramName,paramValue || ""); + return newUrl.toString(); + } else { + return url; + } +}; + })(); diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid index 826ca3032..d42ab4859 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-http-request.tid @@ -18,8 +18,10 @@ The following parameters are used: |!Name |!Description | |method |HTTP method (eg "GET", "POST") | |body |String data to be sent with the request | -|header-* |Headers with string values| +|query-* |Query string parameters with string values | +|header-* |Headers with string values | |password-header-* |Headers with values taken from the password store | +|password-query-* |Query string parameters with values taken from the password store | |var-* |Variables to be passed to the completion and progress handlers (without the "var-" prefix) | |bind-status |Title of tiddler to which the status of the request ("pending", "complete", "error") should be bound | |bind-progress |Title of tiddler to which the progress of the request (0 to 100) should be bound |