Posting Form Data to a Webpage from KM

Hi all,

I'm trying to set up keyboard Maestro to have a macro, when triggered will be able to send and HTTP request to a webpage for processing.

Right now for starters, I created an action - Open URL which goes to a webpage

  • it also contains a simple query string like:

https://mysite.com/process.php?info1=test1&info2=test2

on the page it grabs the query string and does various processes.

So, what I am really looking for is a better way than this to send out an http request as either a GET or better yet a POST request from keyboard Maestro to go to the url.

Question: is something like this possible using keyboard Maestro? If so how what I set this up - (possibly with JavaScript or something)?

You might want to use cURL (https://curl.se/)

Thank you Vincent. I am starting with a previous page I made using javascript XMLHttpRequest().

I have this script currently working on a web page. So I tried copying it into an Execute JavaScript for Automation Action - is this correct?

It has errors that just display briefly small (notifications) - so I can't really read them.
I have the step set to display results large - but I guess that does not show errors.
Is there a way that I can capture the error so I can read it?

I would prefer to get this XMLHttpRequest() going first over cURL if anyone can help with this - thanks

Thanks Dave

Also when I try cURL - what KM action would I use to set it up?

I started looking here:

I set the var KMVAR_My_KM_Variable to 'JOE' in earlier step

Then I tried adding this from demo:

EXAMPLE 1

echo | awk -v my_var=4 '{print "My var is " my_var}'

#-->My var is 4

EXAMPLE 2

Display Text “” Briefly

VAR=3
echo | awk -v env_var="$VAR" '{print "The value of VAR is " env_var}'

#-->The value of VAR is 3

USING KM VARIABLES with awk

echo | awk -v env_var="$KMVAR_My_KM_Variable" '{print "The value of My_KM_Variable is: \""env_var"\""}'

#-->The value of My_KM_Variable is: "Text from KM var"

But not seeing My_KM_Variable yet.

I was trying to do a simple XMLHttpRequest (Post) that works on a web page ok. when I pasted it in KM it gets an error.

Ok I went to the lofts and found this error :

Execute a JavaScript For Automation failed with script error: text-script:141:174: execution error: Error on line 4: ReferenceError: Can't find variable: XMLHttpRequest (-2700) In macro “tests” (while executing Execute JavaScript For Automation).

This is the code test:

var url = 'https://mysite.com/test.php';
var info = {First:"John", Last:"Doe", Company:"TestCo"};	
	
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//document.getElementById("demo").innerHTML = this.responseText;
}
};

xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-type", "application/json;charset=UTF-8");	
xhttp.send(JSON.stringify(info));

Have you tested this using the Apple Script Editor?
If not, then I'd recommend doing so.

Also, please use the Forum Code Block to post all scripts. I have revised your post to do this, this time.

OK I will look into this - and thanks for the codeblock info

Hey @dealtek,

Awk is a bit of a special case and not a great example of working normally with Keyboard Maestro variables in the shell. I'll expand the wiki later with a section devoted to Bash.

The Keyboard Maestro variable is My_KM_Variablenot  KMVAR_My_KM_Variable

The KMVAR_ prefix is necessary to use in the shell.

This example has nothing to do with Keyboard Maestro. It is simply demonstrating using a variable in awk.

I have revised the examples on the wiki and hopefully have improved their clarity. (Work in progress.)

This example too has nothing to do with Keyboard Maestro. It demonstrates using a shell variable in an awk script.

Finally we've come to the point of using the first two examples to work with a Keyboard Maestro variable.

I believe the reason you're not seeing the desired output is as I stated at the beginning:

If not please let me know.

I'll post some more about working with the shell when I have more time.

-Chris