API syntax problem

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

API syntax problem

Post by glenn9 » Fri Apr 08, 2022 3:00 pm

Dear All,

I admit not a LC issue but I just wanted to ask just in case someone had any hints on how to solve this problem.

I`m wanting to call an Oxford English Dictionary API but not sure where to put my app_id and app_key info.

The API stem is simple enough: https://od-api.oxforddictionaries.com/api/v2
and is then followed by the search word: https://od-api.oxforddictionaries.com/api/v2/livecode

They give of examples of how to call the API in other languages, for example in python it`s (https://developer.oxforddictionaries.co ... ng_started):

import requests
import json
app_id = "<your_app_id>"
app_key = "<your_app_key>"
language = "en-gb"
word_id = "example"
url = "https://od-api.oxforddictionaries.com:4 ... 2/entries/" + language + "/" + word_id.lower()
r = requests.get(url, headers={"app_id": <your_app_id>, "app_key": <your_app_key>})

but I was unable to adapt this for use in LC.

I guess what I need is something like:

Code: Select all

https://od-api.oxforddictionaries.com/api/v2/entries/en-gb/livecode/app_id=XXX/app_key=XXX
which I could then use in LC with JSONToArray etc but the syntax I guess is wrong for where I have placed the app_ID and key.

A long shot i know.... but in case anyone does know....!

Thanks,

Glenn

Emily-Elizabeth
Posts: 101
Joined: Mon Jan 03, 2022 7:10 pm

Re: API syntax problem

Post by Emily-Elizabeth » Fri Apr 08, 2022 3:43 pm

The App ID and App Key need to go in the HTTP header, so try setting them with the httpHeaders and see if that works

stam
Posts: 2682
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: API syntax problem

Post by stam » Fri Apr 08, 2022 4:37 pm

What @Emily-Elizabeth said...
the httpHeaders is return-delimited list of text data, you can just set the text text of this and it will be used in http calls like GET

you can search this forum (probably best via google!) for examples as many have done this and posted examples.

On related note, you may also want to use Postman (app or webpage) to test your API calls. Not related to LC directly but you can ensure the calls you make and the results you get make sense... Can't recommend it enough: https://www.postman.com

in LC this should look something like this (more or less):

Code: Select all

on mouseUp
     local tLanguage, tWordID, tHeaders, tURL
     
     put "app_id:XXXXXXX"  & return into tHeaders
     put "app_key:YYYYYYY" after tHeaders
     set the httpHeaders to tHeaders
     
     put "en-gb" into tLanguage
     put "example" into tWordID
     put "https://od-api.oxforddictionaries.com:443/api/v2/entries/" & tLanguage & slash & toLower(tWordID) into tURL
   
     get URL tURL
     put it into field 1
end mouseUp
HTH
S.

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: API syntax problem

Post by glenn9 » Fri Apr 08, 2022 10:01 pm

Emily-Elizabeth, Stam,

Many thanks indeed.

I was completely unaware of the httpHeaders property so this was a great help and I can now see how this is needed and works.

Stam, very many thanks for the code example, this works perfectly and enables me to see how to put it together in practice.

Regards,

Glenn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”