User Tools

Site Tools


Sidebar

dictionary:more:loadurl

LoadURL

Sends an asynchronous web request and returns the result into a special handler in your script. Unlike calling “CURL” from a “do shell script” command this will not wait for the response inline and hang up the entire program while you wait. So you have to write your script in 2 parts. One that starts the request and another that receives the data and does whatever you need to with it. If the request is just to send some data and you do not require a response you can ignore that portion.

loadURL can connect to secure web sites over SSL by just making sure the URL parameter begins with “https:“ instead of just “http:

Usage:

LoadURL (text: the URL to connect to. ie: “https://www.apple.com:80/?query=what”)
The url parameter is required.

Optional Parameters:

timeout (number: the number of seconds to wait before returning an error. Defaults to 30 seconds)
user (text: if required for basic or digest authentication)
password (text: if required for basic or digest authentication)
post data (list: an applescript list of name=value pairs to be POSTED to the server. To send GET parameters include them in the url string as shown above. The list would look something like {“name=value”, “name2=value2”, “name2=value3”,…} )
callback script (text: the name of the global script to send the results to) If you leave this option out but include the handler name then the (thisScript) property will be used to try to call back into the same script that ran the loadURL command.
callback handler (text: the name of the handler in the global script handler to send the response, or error, to.)

Callback Handler Info:

the callback handler can be in any script. It may make sense to place it in the same global script that started the process by running the loadURL command in the first place. It’s name can be anything as long as it’s a valid applescript handler name, so no spaces or non-letter characters. It will have 2 parameters. The first is a number and will contain the return code from the server, or a -1 if there was a timeout. The second parameter is the text result of the request. The callback script and handler are optional parameters. If you do not need to process the reply or aren’t interested in errors then you can leave those parameters out of the loadURL command and the results will be ignored.

an example callback handler:

on myCallback( theResult, theData)
  if theResult is -1 then
    write log “there was a timeout trying to connect to my server” color red
  else if theResult is 200 then
    write log “my server replied with “ & theData
  else
    write log “there was an error returned from the server: “ & theResult
  end if
end myCallback

Examples:

LoadURLhttps://www.apple.com/?these=are&get=parameterstimeout 120 post data {“name=value”, “name=value”} callback script “my global handler” callback handler “myCallback”

dictionary/more/loadurl.txt · Last modified: 2017/04/24 15:35 (external edit)