Add "HTTP request" operation

This commit is contained in:
toby 2017-06-07 22:42:37 -04:00
parent f1e7bc3363
commit a5f1c430a3
2 changed files with 90 additions and 0 deletions

View File

@ -3388,6 +3388,39 @@ const OperationConfig = {
}
]
},
"HTTP request": {
description: [
"Makes a HTTP request and returns the response body.",
"<br><br>",
"This operation supports different HTTP verbs like GET, POST, PUT, etc.",
"<br><br>",
"You can add headers line by line in the format <code>Key: Value</code>",
"<br><br>",
"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;

View File

@ -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;