Getting the Results of a JSON/XML Query

Hi!

Let me first point at that I'm new to KM and also to JSON request.

Here's my scenario:

I want to select a word and then copy it to the clipboard and use "Set Variable "xyz" to Text "SystemClipboard%" in order to use the openthesaurus.de API (www.openthesaurus.de/about/api) to get synonyms for the selected word.

I've then passed this to "Get URL "..." with "display results in a window, but all I get is nothing.

This is what it looks like:
49

I see two possible issues here. (I'm no expert on this topic.)

First, you aren't passing the variable into the Get URL action. Your variable name is varSyn and you don't pass the variable anywhere into the URL.

Second, the Get URL action appears to be intended for use to get text or an image. I'm not really sure if it can work for you in this way. (I'm not a KM expert, just an average user.) I think you may need to convert this action to a different one called Execute Shell Script, and then pass it to the curl command. Like this:

I'm 90% sure that this approach will work, I'm not sure if the Get URL approach will work.

Thanks for your advice!

I have changed it to this:
33

Unfortunately no file is created.

Executing this in the terminal results to this error:
curl: option --ouput: is unknown

It appears that you typed ouput instead of output. That's an error you can fix.

Hey Tobias,

You missed the Keyboard Maestro shell variable format in @Sleepy's post #2 (link).

The format is:

$KMVAR_YourVariableName

Your script is leaving off the $KMVAR_ prefix.

See: Shell Scripting on the wiki.

This macro works:

Get Synonyms from OpenThesaurus.de v1.00.kmmacros (4.7 KB)

I don't understand why you're trying to output a JSON string to a JPG file; that's not going to work...

NOTE:

It's never a good idea to compose a complex command from various parts at run-time.

It's better to compose the command string separately as I've done in the myQuery variable of my script, because this is harmless and can be easily visualized for initial testing and debugging.

Once you've composed your command string and checked to make sure it's correct, then you can run it.

It's way too easy to make cataclysmic errors in the shell, so one should make every effort to avoid them.

-Chris

Thanks for correcting me ccstone, I looked carefully but just missed that.

Thank you all for the help!

Now it seems to work, however there is a server-side error now:
18

Hey Tobias,

Oh, I think that's probably caused by diacritics in the word being looked-up.

This new version of my macro fixes that.

-Chris


Get Synonyms from OpenThesaurus.de Using Curl v1.01.kmmacros (5.7 KB)

I looked at your command, manually typed it into a Terminal window, like this:

curl -sL --url "https://www.openthesaurus.de/synonyme/search?q=Synonyms&format=application/json"

and did not get that error. I got this: (even when I typed it in using your script variable syntax)

{"metaData":{"apiVersion":"0.2","warning":"ACHTUNG: Bitte vor ernsthafter Nutzung feedback@openthesaurus.de kontaktieren, um bei API-\u00c4nderungen informiert zu werden","copyright":"Copyright (C) 2019 Daniel Naber (www.danielnaber.de)","license":"Creative Commons Attribution-ShareAlike 4.0 or GNU LESSER GENERAL PUBLIC LICENSE Version 2.1","source":"https://www.openthesaurus.de","date":"Sun Jun 30 10:12:30 CEST 2019"},"synsets":[]}

Is that the correct result? Perhaps you should try again, maybe the website was down. Or perhaps what you've got in your actions isn't what we can see. For example you may have some trailing spaces or CRs or tabs or something like that which is messing you up. In that case the only thing I can suggest is deleting the text in each of the two last actions in your macro and type them again carefully. Do not copy and paste to re-create them.

If the data above from the website is the correct answer, then it appears likely that the problem is your text includes odd characters that we cannot see. Because the text works for me.

It is correct, when you use German words.

@ccstone: Thank you so much for your Macro!

@ccstone: I realized that nothing did happen at the end of the macro – no window is shown.
I've changed it to

save results to clipboard

but also nothing is changing.

Trying the command in the terminal seems to work...

Chris is always smarter than me, but I have an idea here. When you use it in a terminal window, you say it works, but how long does it take to get results? The problem is that the curl command may be timing out, returning an empty string. You can force curl to wait by using the parameter "connect-timeout" and specify the minimum number of seconds you want it to wait.

Well, it's instantaneously.
I've tried adding that and used 1 and 5 seconds – no change.

Okay then try curl's --verbose option. That's meant to provide more detailed results. For example if it can't find the website, it tells you that.

I'm not really an expert on curl, I'm just an average guy trying to help.

And I really appreciate your help!

This is what I get:

curl -sL --url "https://www.openthesaurus.de/synonyme/search?q=Unterricht&format=application/json" --connect-timeout 2 --verbose

  • Trying 116.203.3.245...

  • TCP_NODELAY set

  • Connected to www.openthesaurus.de (116.203.3.245) port 443 (#0)

  • ALPN, offering h2

  • ALPN, offering http/1.1

  • Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH

  • successfully set certificate verify locations:

  • CAfile: /etc/ssl/cert.pem

CApath: none

  • TLSv1.2 (OUT), TLS handshake, Client hello (1):

  • TLSv1.2 (IN), TLS handshake, Server hello (2):

  • TLSv1.2 (IN), TLS handshake, Certificate (11):

  • TLSv1.2 (IN), TLS handshake, Server key exchange (12):

  • TLSv1.2 (IN), TLS handshake, Server finished (14):

  • TLSv1.2 (OUT), TLS handshake, Client key exchange (16):

  • TLSv1.2 (OUT), TLS change cipher, Client hello (1):

  • TLSv1.2 (OUT), TLS handshake, Finished (20):

  • TLSv1.2 (IN), TLS change cipher, Client hello (1):

  • TLSv1.2 (IN), TLS handshake, Finished (20):

  • SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384

  • ALPN, server accepted to use h2

  • Server certificate:

  • subject: OU=Domain Control Validated; CN=www.openthesaurus.de

  • start date: Oct 1 07:01:09 2018 GMT

  • expire date: Oct 1 07:01:09 2020 GMT

  • subjectAltName: host "www.openthesaurus.de" matched cert's "www.openthesaurus.de"

  • issuer: C=US; ST=Arizona; L=Scottsdale; O=Starfield Technologies, Inc.; OU=Repository; CN=Starfield Secure Certificate Authority - G2

  • SSL certificate verify ok.

  • Using HTTP2, server supports multi-use

  • Connection state changed (HTTP/2 confirmed)

  • Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0

  • Using Stream ID: 1 (easy handle 0x7f9459007a00)

GET /synonyme/search?q=Unterricht&format=application/json HTTP/2

Host: www.openthesaurus.de

User-Agent: curl/7.54.0

Accept: /

  • Connection state changed (MAX_CONCURRENT_STREAMS updated)!

< HTTP/2 200

< server: nginx/1.14.0 (Ubuntu)

< date: Tue, 02 Jul 2019 18:31:41 GMT

< content-type: application/json;charset=utf-8

< x-application-context: application:production

< set-cookie: JSESSIONID=45B3D25F3CAC71D0CE3031FDA836E5CC; Path=/; HttpOnly

< access-control-allow-origin: *

<

{"metaData":{"apiVersion":"0.2","warning":"ACHTUNG: Bitte vor ernsthafter Nutzung feedback@openthesaurus.de kontaktieren, um bei API-\u00c4nderungen informiert zu werden","copyright":"Copyright (C) 2019 Daniel Naber (www.danielnaber.de)","license":"Creative Commons Attribution-ShareAlike 4.0 or GNU LESSER GENERAL PUBLIC LICENSE Version 2.1","source":"https://www.openthesaurus.de","date":"Tue Jul 02 20:31:41 CEST 2019"},"synsets":[{"id":7083,"categories":,"terms":[{"term":"Anleitung"},{"term":"Ausbildung"},{"term":"Belehrung"},{"term":"Lektion"},{"term":"Unterricht"},{"term":"Unterrichtung"}]}]}

So to me it seems to work.... In the terminal, but not in the macro...

Just to be clear, you tried -v in the macro and it still produced no window? I need you to confirm that.

Where, at the end of the curl command?

If you are still getting no results from the Execute Shell Script action after inserting -v, then I think you need to flip on the error switch for the action by clicking on the cogwheel in the action and checking the box for "Include Errors". That may provide more information.

The -v command could go here:

curl -v -sL --url "https://www.openthesaurus.de/synonyme/search?q=Synonyms&format=application/json"

I didn't check if it can go at the end of the curl command.