POST-Anfrage funktioniert in Postman, aber nicht in Python
Wenn ich diese POST-Anfrage in Postman mache, erhalte ich die Daten. Wenn ich es in Python 2.7 (mit einem Jupyter-Notizbuch) mache, erhalte ich die Fehlermeldung "Es konnte kein JSON-Objekt dekodiert werden". Was mache ich falsch und wie kann ich dafür sorgen, dass es funktioniert?
import json
import requests
url = 'http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy'
headers={'content-type': 'application/json'}
payload = {
"query": [
{
"code": "ContentsCode",
"selection": {
"filter": "item",
"values": [
"BE0101N1"
]
}
},
{
"code": "Tid",
"selection": {
"filter": "item",
"values": [
"2010",
"2011"
]
}
},
{
"code": "Region",
"selection": {
"filter": "item",
"values": [
"01"
]
}
}
],
"response": {
"format": "json"
}
}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=payload)
print(r.text)
print(r.json())
Das Handbuch für die API ist hier, aber es ist nicht viel Hilfe:
http: //www.scb.se/en_/About-us/Open-data-API/API-for-the-Statistical-Database-