Skip to main content
Roy Marshall
Known Participant
September 30, 2011
Question

[JS] getting contents from a http request

  • September 30, 2011
  • 1 reply
  • 2226 views

Hi.

I am working with an api accepts a url containing a server address, a user id, a user password and returns a status.

The compiled url looks like this and when pasted into a browser, displays the status id:

http://10.1.1.234/login/SearchService?SERVICE_REQUEST_TYPE=38&SERVICE_REQUEST_USERID=11&SERVICE_REQUEST_ASSETID=13444&SERVICE_REQUEST_PASSKEY=******

but I need to pass this as part of my Javascript, so as far as I can see I need to encapsulate into an Applescript curl command:

do shell script "curl -0  http://10.1.1.234/login/SearchService?SERVICE_REQUEST_TYPE=38&SERVICE_REQUEST_USERID=11&SERVICE_REQUEST_ASSETID=13444&SERVICE_REQUEST_PASSKEY=*******"

which is passed to Applescript by:

myNumber = app.doScript(myEncodedString, ScriptLanguage.APPLESCRIPT_LANGUAGE);

I get an empty string returned. it looks like the informaton isn't being passed as a page that can be read in this way. 

Is there another way I can get a hold of this information that anyone can think of without asking for the api to be re-written? It would be nice to be able to post an http request using js with this api without resorting to writting php code.

Cheers

Roy

This topic has been closed for replies.

1 reply

John Hawkinson
Inspiring
October 1, 2011

Roy:

but I need to pass this as part of my Javascript, so as far as I can see I need to encapsulate into an Applescript curl command:

No, that is not a requirement.

The other approach is to use the Socket object and execute the raw HTTP query using the Socket TCP interface. The Javascript tools guide gives an example of an HTTP GET. You might find it easier, but you might not. YMMV.

It seems clear you have not opened Terminal and tested the curl line you have constructed, that would have been an excellent first debugging step. It should be quite clear what the problem is. Ampersands are special characters to the shell and must be quoted when passed to the shell. Unfortunately you quoting may be eaten by AppleScript, so you may need to use not only \& but perhaps \\& or \\\&. Try testing it with do shell script "echo http://testing?foo\&bar" and see what you get.

John Hawkinson
Inspiring
October 1, 2011

The other approach is to use the Socket object and execute the raw HTTP query using the Socket TCP interface. The Javascript tools guide gives an example of an HTTP GET. You might find it easier, but you might not. YMMV.

Addendum: It is certainly more portable, as shelling out to curl via Applescript won't work under WIndows (for more than one reason). It might also be faster. Certainly fewer fork()s.

Roy Marshall
Known Participant
October 1, 2011

Hi John.

Thanks for the replies.  I did try the curl in a terminal window yesterday, but didn't get the result I was wanting.  I have tried escaping with one, two and three back slashes, but the terminal result seems to stit out 3 lines of results that are split by the "&" sign.

[1]   Exit 127                http://10.1.1.234/login/SearchService?SERVICE_REQUEST_TYPE=38

[2]-  Done                    SERVICE_REQUEST_USERID=11

[3]+  Done                    SERVICE_REQUEST_ASSETID=13444

What I should be getting back from this call, is a single digit "2"

I will look into the socket option, as that has always been a prefered way being cross-platform.

Thanks again

Roy