diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 5fd5a9ee..ddb67d28 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -3388,6 +3388,39 @@ const OperationConfig = { } ] }, + "HTTP request": { + description: [ + "Makes a HTTP request and returns the response body.", + "

", + "This operation supports different HTTP verbs like GET, POST, PUT, etc.", + "

", + "You can add headers line by line in the format Key: Value", + "

", + "This operation will throw an error for any status code that is not 200, unless the 'Ignore status code' option is checked.", + ].join("\n"), + run: HTTP.runHTTPRequest, + inputType: "string", + outputType: "string", + args: [ + { + name: "Method", + type: "option", + value: HTTP.METHODS, + }, { + name: "URL", + type: "string", + value: "", + }, { + name: "Headers", + type: "text", + value: "", + }, { + name: "Ignore status code", + type: "boolean", + value: false, + }, + ] + }, }; export default OperationConfig; diff --git a/src/core/operations/HTTP.js b/src/core/operations/HTTP.js index 28c41ad5..e80c0fb3 100755 --- a/src/core/operations/HTTP.js +++ b/src/core/operations/HTTP.js @@ -11,6 +11,14 @@ import {UAS_parser as UAParser} from "../lib/uas_parser.js"; * @namespace */ const HTTP = { + /** + * @constant + * @default + */ + METHODS: [ + "GET", "POST", "HEAD", + "PUT", "PATCH", "DELETE", + ], /** * Strip HTTP headers operation. @@ -51,6 +59,55 @@ const HTTP = { "Device Type: " + ua.deviceType + "\n"; }, + + /** + * HTTP request operation. + * + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + runHTTPRequest(input, args) { + const method = args[0], + url = args[1], + headersText = args[2], + ignoreStatusCode = args[3]; + + if (url.length === 0) return ""; + + let headers = new Headers(); + headersText.split(/\r?\n/).forEach(line => { + line = line.trim(); + + if (line.length === 0) return; + + let split = line.split(":"); + if (split.length !== 2) throw `Could not parse header in line: ${line}`; + + headers.set(split[0].trim(), split[1].trim()); + }); + + let config = { + method, + headers, + mode: "cors", + cache: "no-cache", + }; + + if (method !== "GET" && method !== "HEAD") { + config.body = input; + } + + return fetch(url, config) + .then(r => { + if (ignoreStatusCode || r.status === 200) { + return r.text(); + } else { + throw `HTTP response code was ${r.status}.`; + } + }); + }, + }; export default HTTP;