[JSON API] Ajax calls using jquery

Suggestions and discussion of future versions
Post Reply
tab1293
New User
New User
Posts: 4
Joined: Fri Nov 02, 2012 9:49 pm

[JSON API] Ajax calls using jquery

Post by tab1293 »

I'm trying to call deluge-web using jquery's $.ajax() function but for some reason it is not working. I think it may be a cross domain request issue. I know the server is definitely working because I get responses when using curl. Here is a sample curl call and its equivalent jquery call (or so I think). Can somebody let me know what it wrong? I get no callback to success or error. Here is my code

http://pastebin.ca/2247771
Cas
Top Bloke
Top Bloke
Posts: 3681
Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland

Re: Ajax calls using jquery

Post by Cas »

As you think it is because of the cross domain request issue, there are many suggestions for how to circumvent on Stackoverflow: http://stackoverflow.com/a/3076648/175584

You may be interested in the deluge greasemonkey script that uses GM_xmlhttpRequest to get around this issue.
tab1293
New User
New User
Posts: 4
Joined: Fri Nov 02, 2012 9:49 pm

Re: Ajax calls using jquery

Post by tab1293 »

Thanks for the response Cas. I was looking into CORS but wasn't sure how to implement it. I was going through deluge's source to see where I could potentially change the header the server sends. If I add a Access-Control-Allow-Origin header in this send_response function do you think that this will fix my problem?
http://git.deluge-torrent.org/deluge/tr ... pi.py#n277
Cas
Top Bloke
Top Bloke
Posts: 3681
Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland

Re: Ajax calls using jquery

Post by Cas »

Yes that would make sense, I am not sure of the exact headers but I think you would be looking to add something like this:

Code: Select all

           request.setHeader('Access-Control-Allow-Origin', '*')
           request.setHeader('Access-Control-Allow-Methods', 'POST')
           request.setHeader('Access-Control-Allow-Headers', 'x-requested-with')
           request.setHeader('Access-Control-Max-Age', 1800)
tab1293
New User
New User
Posts: 4
Joined: Fri Nov 02, 2012 9:49 pm

Re: Ajax calls using jquery

Post by tab1293 »

Cas changing those lines did not work but I ended up getting around the cross domain request issue by setting up a reverse proxy.

I am having another problem now though with the authentication process. Even after I call the auth.login method and successfully authenticate, whenever I try to call any other method I get an error message saying I am not authenticated.

Code: Select all

tom@bergerserver:~/delugeDev$ curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method":"auth.login","params":["deluge"],"id":1}' http://bergerserver.info/deluge/json

HTTP/1.1 200 OK
Date: Sun, 04 Nov 2012 19:35:35 GMT
Server: TwistedWeb/11.1.0
Content-Encoding: gzip
Content-Type: application/x-json
Set-Cookie: _session_id=ebddfce33cc76ea7b593bbcc1a8a568b2576; Expires=Sun, 04 Nov 2012 20:35:35 GMT; Path=/json
Transfer-Encoding: chunked

{"id": 1, "result": true, "error": null}

tom@bergerserver:~/delugeDev$ curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method":"web.get_config","params":[],"id":1}' http://bergerserver.info/deluge/json

HTTP/1.1 200 OK
Date: Sun, 04 Nov 2012 19:36:03 GMT
Server: TwistedWeb/11.1.0
Content-Encoding: gzip
Content-Type: application/x-json
Transfer-Encoding: chunked

{"id": 1, "result": null, "error": {"message": "Not authenticated", "code": 1}}
I though it might be an issue with the cookie time expiration so I changed the timezone of my server to GMT -8 (which is where I am connecting to the server from) but it does not change anything. Any idea what is going wrong?

Also, after I authenticate how can I connect to the deluge daemon from the JSON api? I think I have to use the web.connect method but I don't know what to use as the parameter host_id. What does the host_id represent and where can I find my host_id? Thanks
Cas
Top Bloke
Top Bloke
Posts: 3681
Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland

Re: Ajax calls using jquery

Post by Cas »

The curl code you have supplied needs to create the cookie file (-c) rather than read it (-b) with auth.login.

Use get_hosts to get a list:
http://deluge-torrent.org/docs/master/m ... .get_hosts
tab1293
New User
New User
Posts: 4
Joined: Fri Nov 02, 2012 9:49 pm

Re: Ajax calls using jquery

Post by tab1293 »

Cas, I used the -c flag with auth.login and the -b flag to read it with the following calls but I am still getting the not authenticated error.

Code: Select all

tom@bergerserver:~$ curl -c cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method":"auth.login","params":["deluge"],"id":1}' http://bergerserver.info/deluge/json

HTTP/1.1 200 OK
Date: Sun, 04 Nov 2012 21:47:32 GMT
Server: TwistedWeb/11.1.0
Content-Encoding: gzip
Content-Type: application/x-json
Set-Cookie: _session_id=2c1b2b7f7974c181d5dc8787077bb36f2208; Expires=Sun, 04 Nov 2012 22:47:32 GMT; Path=/json
Transfer-Encoding: chunked

tom@bergerserver:~$ curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method":"web.get_config","params":[],"id":1}' http://bergerserver.info/deluge/json

HTTP/1.1 200 OK
Date: Sun, 04 Nov 2012 21:48:02 GMT
Server: TwistedWeb/11.1.0
Content-Encoding: gzip
Content-Type: application/x-json
Transfer-Encoding: chunked

{"id": 1, "result": null, "error": {"message": "Not authenticated", "code": 1}}
Here is my cookies.txt:

Code: Select all

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

bergerserver.info   FALSE   /json   FALSE   1352069252  _session_id 2c1b2b7f7974c181d5dc8787077bb36f2208
Any help?

EDIT: The problem is in the cookies.txt file, the directory /json should be /deluge/json to match my reverse proxy. Is there anyway to configure deluge to change the JSON api path to something else besides /json? I can always just change my reverse proxy if not.
Cas
Top Bloke
Top Bloke
Posts: 3681
Joined: Mon Dec 07, 2009 6:04 am
Location: Scotland

Re: Ajax calls using jquery

Post by Cas »

Maybe this will help you, it was puzzling me why the CORS addition did not work and found that its due to ignoring the initial OPTIONS method:

Code: Select all

diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py
index 23fb318..a9f8414 100644
--- a/deluge/ui/web/json_api.py
+++ b/deluge/ui/web/json_api.py
@@ -277,6 +277,7 @@ def _on_json_request_failed(self, reason, request):
     def _send_response(self, request, response):
         response = json.dumps(response)
         request.setHeader("content-type", "application/x-json")
+        request.setHeader('Access-Control-Allow-Origin', '*')
         request.write(compress(response, request))
         request.finish()
 
@@ -284,6 +285,12 @@ def render(self, request):
         """
         Handles all the POST requests made to the /json controller.
         """
+        if request.method == "OPTIONS":
+            request.setHeader('Access-Control-Allow-Origin', '*')
+            request.setHeader('Access-Control-Allow-Methods', 'POST')
+            request.setHeader('Access-Control-Allow-Headers', 'Content-type')
+            request.setHeader('Access-Control-Max-Age', 1800)
+            return ""
 
         if request.method != "POST":
             request.setResponseCode(http.NOT_ALLOWED)

Post Reply