I am using below curl command to get the data from Druid cluster hosted on remote server, in the json format
curl -X POST "http://www.myserverIP.com:8080/druid/v2/" -H 'content-type: application/json' -d '{"queryType": "groupBy","dataSource": "twsample","granularity": "none","dimensions": ["created_at"],"aggregations": [{"type": "count", "name": "tweetcount"}],"intervals": ["2013-08-06T11:30:00.000Z/2020-08-07T11:40:00.000Z"]}'
but I am not able create an equivalent javascript method which will do the same thing, I am using below code to do this.
function sendCurlRequest(){
var json_data = {
"queryType": "groupBy",
"dataSource": "twsample",
"granularity": "none",
"dimensions": ["created_at"],
"aggregations": [
{"type": "count", "name": "tweetcount"}
],
"intervals": ["2013-08-06T11:30:00.000Z/2020-08-07T11:40:00.000Z"]
};
$.ajax({
cache : false,
type: 'POST',
crossDomain:true,
url: 'http://www.myserverIP:8080/druid/v2/',
data:json_data,
//dataType: "jsonp",
contentType:"application/jsonp",
success: function(data){
alert(data);
var pubResults = data;
},
error: function(data){
alert("ERROR RESPONSE FROM DRUID SERVER : "+JSON.stringify(data));
},
complete: function(data){
console.log("call completed");
}
});
}
Any help will be appreciated.
Curl is a popular command line tool often used for making HTTP requests. Curl supports a wide variety of other protocols, but, as a Node. js developer, you'll most likely use it to make HTTP requests to RESTful APIs.
To perform a curl request in JavaScript, we can use the fetch function. We call fetch with the URL we want to make the request to to make a GET request. Then we call res. json to convert the response body object to JSON.
Curl is a way you can hit a URL from your code to get an HTML response from it. In NodeJS/ExpressJS we use “request” module for this purpose. In Simple Words, if we want to access HTML content of any site we pass that URL to request( ) and it returns the HTML source code.
To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.
cURL is able to send and receive data cross domain to remote servers but that is not the case with javascript for security reasons
Your options I believe are
1) Use CORS
to set headers on the remote server to accept cross domain calls (if you have access to the Server) as also pointed by thriqon
2) Use jsonP
format response from the server(again if you have control over how the response is generated or if its already in jsonP format)
3) Write a server-side proxy
that acts as the intermediary between your calls and the remote server. You can then format the header or response on the proxy to enable it to respond to cross domain calls
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With