Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

[JS] getting contents from a http request

Participant ,
Sep 30, 2011 Sep 30, 2011

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_REQU...

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_REQU..."

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

TOPICS
Scripting
2.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2011 Sep 30, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2011 Sep 30, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 01, 2011 Oct 01, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 01, 2011 Oct 01, 2011

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

Show me what you actually tried!

Here is what I see:

$ echo http://testing?foo&bar&baz

[22] 6703

[23] 6704

http://testing?foo

-bash: bar: command not found

-bash: baz: command not found

[22]   Done                    echo http://testing?foo

[23]   Exit 127                bar

$ echo http://testing?foo\&bar\&baz

http://testing?foo&bar&baz

$

The output you supplied is consistent with not quoting the &, but you said that you tried it.

Show us what you did.


However, there is a larger concern:

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

I have been sorely remiss in my answer -- user input is involved here? Then you have to worry about quoting the user input! What if the user input contains pass`rm -rf /`word? Then your hard drive will be deleted (well, ok, only files you have write permission on) if this executes, unless you quote the backticks. and there are scores of shell metacharacters to worry about. Passing user input to the shell is a very ba didea,i it's quite easy to generate security concerns. (Though you'll have some with the Socket object too, I guess, but not as severe).

This gets worse with two level of quoting (applescript).

I typically use this function, not designed for security, to address some quoting issues from Javascript to Applescript via shell:

function shell(cmd) {

        var

                rv,

                call ='do shell script "'+

                        cmd.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+

                        '"';

        try {

                rv = app.doScript(call,

                        ScriptLanguage.APPLESCRIPT_LANGUAGE);

        } catch(e0) {

                rv = e0+"\n"+(rv?"":rv);

        }

        return rv;

}

But again, it's not designed to fix user input issues, merely to avoid backslash and double-quote issues in Applescript.

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

great. Though it sounds like you're having trouble with it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 02, 2011 Oct 02, 2011

HI.

It turns out that I using curl, through 2 layers on Do Script withe the specific API I need to use will not work.

After a lot of trial and error, I resorted to calling on the help of my 15yr old son, who came up with I need to do. What I have needed to do is pass a string containing all the variables needed seaparated by a delimter, to a PHP script that cat re-construct the URL with success.

This works perfectly, but still needs a doscript sequence (JS > AS > SHELL) to pas the url containing the variables.

Now to look at the socket solution...

Roy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 02, 2011 Oct 02, 2011

Hi.

I have spent some  time playing with the code below:

reply = "";

conn = new Socket();

conn.open("localhost:80");

conn.write(" GET /getassetstatus.php?enc=10.1.1.234~38~11~13446~PASSWORD");

reply = conn.read(999999);

conn.close;

where the php file on my computer "getassetstatus.php" takes the string, and returns a number.  This works when being passed as a curl commend 2 layers deep inside doScript, but I want to take out the Applescript dependancy.

I dont know what the "GET" does, and the code I used as a base had "HTTP/1.0\n\n" at the end of the url.

With all my tests, the variable reply is always empty.

If anyone can help, I would be grateful.

Cheers

Roy

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 02, 2011 Oct 02, 2011
LATEST

Answered in your other thead: Re: Getting data from SQL database.

Please don't post the same question twice.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines