Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Did anybody try to request the data via Python? #45

Closed
JohnnyFruitbasket opened this issue Apr 29, 2024 · 3 comments
Closed

Did anybody try to request the data via Python? #45

JohnnyFruitbasket opened this issue Apr 29, 2024 · 3 comments

Comments

@JohnnyFruitbasket
Copy link

So far, all I am able to do is produce "JSONDecodeError"s: "Expecting value: line 1 column 1 (char 0)"

I tried something like this:

import requests

api_url = "https://statcubeapi.statistik.at/statistik.at/ext/statcube/rest/v1e/table"

api_key = "<quitesomekeyhere>"

headers = {'APIKey': api_key, "Content-Type": "application/json"}

query = {
  "database" : "str:database:debevstprog",
  "measures" : [ "str:statfn:debevstprog:F-BEVSTPROG:F-S25V1:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V2:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V3:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V4:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V5:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V6:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V7:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V8:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V9:SUM", "str:statfn:debevstprog:F-BEVSTPROG:F-S25V10:SUM" ],
  "recodes" : {
    "str:field:debevstprog:F-BEVSTPROG:C-C11-0" : {
      "map" : [ [ "str:value:debevstprog:F-BEVSTPROG:C-C11-0:C-C11-0:C11-1" ], [ "str:value:debevstprog:F-BEVSTPROG:C-C11-0:C-C11-0:C11-2" ] ],
      "total" : False
    },
    "str:field:debevstprog:F-BEVSTPROG:C-A10-0" : {
      "map" : [ [ "str:value:debevstprog:F-BEVSTPROG:C-A10-0:C-A10-0:A10-2000" ], [ "str:value:debevstprog:F-BEVSTPROG:C-A10-0:C-A10-0:A10-2010" ], [ "str:value:debevstprog:F-BEVSTPROG:C-A10-0:C-A10-0:A10-2020" ], [ "str:value:debevstprog:F-BEVSTPROG:C-A10-0:C-A10-0:A10-2030" ] ],
      "total" : False
    }
  },
  "dimensions" : [ [ "str:field:debevstprog:F-BEVSTPROG:C-A10-0" ], [ "str:field:debevstprog:F-BEVSTPROG:C-C11-0" ] ]
}

response = requests.get(api_url, headers=headers, params=query)
response_data = response.json()
print(response_data.text)

Also had to replace the "false" in the JSON query with "False", as the IDE was throwing an error otherwise. I am not the most firm with REST APIs, can anybody offer some insight how this might work?

@GregorDeCillia
Copy link
Contributor

I don't think params is the way to pass the json payload. I am not familiar with python, but I think params will convert the data from query into query parameters while the API expects the data in the request body. Also, POST is the expected http verb for the /table endpoint, not GET. According to this SO question, this should work.

requests.post(api_url, headers=headers, json=query)

@JohnnyFruitbasket
Copy link
Author

JohnnyFruitbasket commented Apr 29, 2024

Thanks, that was quite useful. I made some changes (turns out, there is an json parameter in the .post method) and this does it for me:

response = requests.post(api_url, headers=headers, json=query)
print(response.status_code)
response_data = response.json()
print(response_data)

Got a positive status code and the requested data. Also had a typo in the URL, it is "v1" and not "v1e".

@GregorDeCillia
Copy link
Contributor

It's a convention that GET requests only pass request-data via query-parameters. That's why the json parameter is not available in .get() but available in .post(). Anyways, glad to hear this is working for you now. In case this comes up: invalid JSON and rate limits might generate misleading error messages. I've written up a guide for this in https://statistikat.github.io/STATcubeR/dev/articles/sc_last_error.html#invalid-json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants