Translate CURL command to python code for fetching data from Cloudflare API












0















I tried to create python code for fetching api data of CloudFlare analytics dashboard. This is how I translated the following Curl command to Python:



I want to query out data for time duration 1 day(from 2018-11-18 00:00:00 till 2018-11-19 00:00:00).



Curl command:



curl -X GET "https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true" 
-H "Content-Type:application/json"
-H "X-Auth-Key:1234567893feefc5f0q5000bfo0c38d90bbeb"
-H "X-Auth-Email:example@example.com"


Python Code:



  import http.client, json

def find_data():
conn = http.client.HTTPSConnection("api.cloudflare.com")
headers = {
'Content-Type': "application/json",
'X-Auth-Email': "example@example.com",
'X-Auth-Key': "1234567893feefc5f0q5000bfo0c38d90bbeb",
}
payload = "since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true"
conn.request("POST", "/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard", payload, headers)
res = conn.getresponse()
data = res.read().decode('utf-8')
loadedjson = json.loads(data)
return(loadedjson)
print(find_data())


Problem:



The script is working but i am getting data form 17 Nov till the current date i.e. i am not getting the queried data from 18-19 Nov but 17-24.



Is this the correct way to query the data from cloud flare, if not please correct it. If there is some other way in python to fetch data from API please modify the script.










share|improve this question

























  • In your translation you've changed the method from GET to POST. You've also named your query parameter variable payload which may be affecting your ability to search for the correct syntax. The reason you are seeing 1 week of data in the past is because that is the default for the api if it does not receive optional params in the form of a query string. I don't know python so can't give you the code but you're close!

    – jdurkin
    Nov 24 '18 at 14:36






  • 1





    Reconsider switching to python-requests

    – stovfl
    Nov 24 '18 at 14:45
















0















I tried to create python code for fetching api data of CloudFlare analytics dashboard. This is how I translated the following Curl command to Python:



I want to query out data for time duration 1 day(from 2018-11-18 00:00:00 till 2018-11-19 00:00:00).



Curl command:



curl -X GET "https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true" 
-H "Content-Type:application/json"
-H "X-Auth-Key:1234567893feefc5f0q5000bfo0c38d90bbeb"
-H "X-Auth-Email:example@example.com"


Python Code:



  import http.client, json

def find_data():
conn = http.client.HTTPSConnection("api.cloudflare.com")
headers = {
'Content-Type': "application/json",
'X-Auth-Email': "example@example.com",
'X-Auth-Key': "1234567893feefc5f0q5000bfo0c38d90bbeb",
}
payload = "since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true"
conn.request("POST", "/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard", payload, headers)
res = conn.getresponse()
data = res.read().decode('utf-8')
loadedjson = json.loads(data)
return(loadedjson)
print(find_data())


Problem:



The script is working but i am getting data form 17 Nov till the current date i.e. i am not getting the queried data from 18-19 Nov but 17-24.



Is this the correct way to query the data from cloud flare, if not please correct it. If there is some other way in python to fetch data from API please modify the script.










share|improve this question

























  • In your translation you've changed the method from GET to POST. You've also named your query parameter variable payload which may be affecting your ability to search for the correct syntax. The reason you are seeing 1 week of data in the past is because that is the default for the api if it does not receive optional params in the form of a query string. I don't know python so can't give you the code but you're close!

    – jdurkin
    Nov 24 '18 at 14:36






  • 1





    Reconsider switching to python-requests

    – stovfl
    Nov 24 '18 at 14:45














0












0








0








I tried to create python code for fetching api data of CloudFlare analytics dashboard. This is how I translated the following Curl command to Python:



I want to query out data for time duration 1 day(from 2018-11-18 00:00:00 till 2018-11-19 00:00:00).



Curl command:



curl -X GET "https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true" 
-H "Content-Type:application/json"
-H "X-Auth-Key:1234567893feefc5f0q5000bfo0c38d90bbeb"
-H "X-Auth-Email:example@example.com"


Python Code:



  import http.client, json

def find_data():
conn = http.client.HTTPSConnection("api.cloudflare.com")
headers = {
'Content-Type': "application/json",
'X-Auth-Email': "example@example.com",
'X-Auth-Key': "1234567893feefc5f0q5000bfo0c38d90bbeb",
}
payload = "since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true"
conn.request("POST", "/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard", payload, headers)
res = conn.getresponse()
data = res.read().decode('utf-8')
loadedjson = json.loads(data)
return(loadedjson)
print(find_data())


Problem:



The script is working but i am getting data form 17 Nov till the current date i.e. i am not getting the queried data from 18-19 Nov but 17-24.



Is this the correct way to query the data from cloud flare, if not please correct it. If there is some other way in python to fetch data from API please modify the script.










share|improve this question
















I tried to create python code for fetching api data of CloudFlare analytics dashboard. This is how I translated the following Curl command to Python:



I want to query out data for time duration 1 day(from 2018-11-18 00:00:00 till 2018-11-19 00:00:00).



Curl command:



curl -X GET "https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true" 
-H "Content-Type:application/json"
-H "X-Auth-Key:1234567893feefc5f0q5000bfo0c38d90bbeb"
-H "X-Auth-Email:example@example.com"


Python Code:



  import http.client, json

def find_data():
conn = http.client.HTTPSConnection("api.cloudflare.com")
headers = {
'Content-Type': "application/json",
'X-Auth-Email': "example@example.com",
'X-Auth-Key': "1234567893feefc5f0q5000bfo0c38d90bbeb",
}
payload = "since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true"
conn.request("POST", "/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard", payload, headers)
res = conn.getresponse()
data = res.read().decode('utf-8')
loadedjson = json.loads(data)
return(loadedjson)
print(find_data())


Problem:



The script is working but i am getting data form 17 Nov till the current date i.e. i am not getting the queried data from 18-19 Nov but 17-24.



Is this the correct way to query the data from cloud flare, if not please correct it. If there is some other way in python to fetch data from API please modify the script.







python authentication cloudflare






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 13:35









stovfl

7,49931031




7,49931031










asked Nov 24 '18 at 13:58









Salik47Salik47

1916




1916













  • In your translation you've changed the method from GET to POST. You've also named your query parameter variable payload which may be affecting your ability to search for the correct syntax. The reason you are seeing 1 week of data in the past is because that is the default for the api if it does not receive optional params in the form of a query string. I don't know python so can't give you the code but you're close!

    – jdurkin
    Nov 24 '18 at 14:36






  • 1





    Reconsider switching to python-requests

    – stovfl
    Nov 24 '18 at 14:45



















  • In your translation you've changed the method from GET to POST. You've also named your query parameter variable payload which may be affecting your ability to search for the correct syntax. The reason you are seeing 1 week of data in the past is because that is the default for the api if it does not receive optional params in the form of a query string. I don't know python so can't give you the code but you're close!

    – jdurkin
    Nov 24 '18 at 14:36






  • 1





    Reconsider switching to python-requests

    – stovfl
    Nov 24 '18 at 14:45

















In your translation you've changed the method from GET to POST. You've also named your query parameter variable payload which may be affecting your ability to search for the correct syntax. The reason you are seeing 1 week of data in the past is because that is the default for the api if it does not receive optional params in the form of a query string. I don't know python so can't give you the code but you're close!

– jdurkin
Nov 24 '18 at 14:36





In your translation you've changed the method from GET to POST. You've also named your query parameter variable payload which may be affecting your ability to search for the correct syntax. The reason you are seeing 1 week of data in the past is because that is the default for the api if it does not receive optional params in the form of a query string. I don't know python so can't give you the code but you're close!

– jdurkin
Nov 24 '18 at 14:36




1




1





Reconsider switching to python-requests

– stovfl
Nov 24 '18 at 14:45





Reconsider switching to python-requests

– stovfl
Nov 24 '18 at 14:45












1 Answer
1






active

oldest

votes


















0














import requests

headers = {
'Content-Type': 'application/json',
'X-Auth-Key': '1234567893feefc5f0q5000bfo0c38d90bbeb',
'X-Auth-Email': 'example@example.com',
}

response = requests.get('https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true', headers=headers)





share|improve this answer
























  • Thanks, it worked! only have to add response.json()

    – Salik47
    Nov 25 '18 at 12:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458889%2ftranslate-curl-command-to-python-code-for-fetching-data-from-cloudflare-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














import requests

headers = {
'Content-Type': 'application/json',
'X-Auth-Key': '1234567893feefc5f0q5000bfo0c38d90bbeb',
'X-Auth-Email': 'example@example.com',
}

response = requests.get('https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true', headers=headers)





share|improve this answer
























  • Thanks, it worked! only have to add response.json()

    – Salik47
    Nov 25 '18 at 12:15
















0














import requests

headers = {
'Content-Type': 'application/json',
'X-Auth-Key': '1234567893feefc5f0q5000bfo0c38d90bbeb',
'X-Auth-Email': 'example@example.com',
}

response = requests.get('https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true', headers=headers)





share|improve this answer
























  • Thanks, it worked! only have to add response.json()

    – Salik47
    Nov 25 '18 at 12:15














0












0








0







import requests

headers = {
'Content-Type': 'application/json',
'X-Auth-Key': '1234567893feefc5f0q5000bfo0c38d90bbeb',
'X-Auth-Email': 'example@example.com',
}

response = requests.get('https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true', headers=headers)





share|improve this answer













import requests

headers = {
'Content-Type': 'application/json',
'X-Auth-Key': '1234567893feefc5f0q5000bfo0c38d90bbeb',
'X-Auth-Email': 'example@example.com',
}

response = requests.get('https://api.cloudflare.com/client/v4/zones/cd7d0123e3012345da9420df9514dad0/analytics/dashboard?since=2018-11-18T00:00:00Z&until=2018-11-19T00:00:00Z&continuous=true', headers=headers)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 20:11









Itay4Itay4

1731213




1731213













  • Thanks, it worked! only have to add response.json()

    – Salik47
    Nov 25 '18 at 12:15



















  • Thanks, it worked! only have to add response.json()

    – Salik47
    Nov 25 '18 at 12:15

















Thanks, it worked! only have to add response.json()

– Salik47
Nov 25 '18 at 12:15





Thanks, it worked! only have to add response.json()

– Salik47
Nov 25 '18 at 12:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458889%2ftranslate-curl-command-to-python-code-for-fetching-data-from-cloudflare-api%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista