Support Questions
Find answers, ask questions, and share your expertise

How to rewrite this Curl call (to the Apache Hue REST API) as (JQuery) AJAX?

Highlighted

How to rewrite this Curl call (to the Apache Hue REST API) as (JQuery) AJAX?

New Contributor

I need to call the Apache Hue REST API via AJAX calls (using JQuery for example) and so I need to convert the following curl call to AJAX (taken from https://docs.gethue.com/developer/api/#execute-a-query )

curl -X POST https://demo.gethue.com/notebook/api/execute/hive --data 'executable={"statement":"SHOW TABLES","database":"default"}&notebook={"type":"query","snippets":[{"id":1,"statement_raw":"SHOW TABLES","type":"hive","variables":[]}],"name":"","isSaved":false,"sessions":[]}&snippet={"id":1,"type":"hive","result":{},"statement":"SHOW TABLES","properties":{}}'

(obviously I'm using the URL of my Hue installation and my query would be something like "SELECT * FROM my_table LIMIT 10;")

This was one of my attempts, but I always get back an error in the console {status: -1, message: "'type'"}

$.ajax({
    url: "/notebook/api/execute/hive/",
    type: 'POST',
    processData: false,
    data: {"executable": JSON.stringify({"statement":"SELECT * FROM my_table LIMIT 10","database":"default"}), "notebook": JSON.stringify({"type":"query","snippets":[{"id":1,"statement_raw":"SELECT * FROM my_table LIMIT 10","type":"hive","variables":[]}],"name":"","isSaved":false,"sessions":[]}), "snippet": JSON.stringify({"id":1,"type":"hive","result":{},"statement":"SELECT * FROM my_table LIMIT 10","properties":{}})},
    success: function(data) { console.log(data); },
    error: function(err) { console.log(err); }
});

If I remove the processData: false line I get the error: 

 

{status: -1, message: "'handle'"}

 

However I tried the following in Chrome devtools console (I am logged into Hue via the web interface on the same page as the console) and it works fine, so I know the above should work if I can get the syntax right:

$.ajax({
    url: "/notebook/api/autocomplete/",
    type: 'POST',
    data: {"snippet": JSON.stringify({"type": "hive"})},
    success: function(data) {console.log(data); }
});

For reference, the curl equivalent of that is here (although it seems I don't need to specify CRSF or sessionID because I'm already logged in via the interface in the same browser window as the console):

curl -X POST https://demo.gethue.com/notebook/api/autocomplete/ --data 'snippet={"type":"hive"}' --cookie "csrftoken=XUFgN1WPZNlaJtBeBDtBvwzrOFqRXIaMlNJv4mdvsS2bIE2Lb8LRmCh5cPUBnBdk;sessionid=9cdltfee1q1zmt8b7slsjcomtxzgvgfz" -H "X-CSRFToken: XUFgN1WPZNlaJtBeBDtBvwzrOFqRXIaMlNJv4mdvsS2bIE2Lb8LRmCh5cPUBnBdk

Any suggestions very welcome! Thanks