POST请求在Postman中工作,但在Python中不工作

2024-09-23 00:18:33 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在邮递员那里提出这个邮件请求时,我得到了数据。当我在Python2.7(使用Jupyter笔记本)中执行此操作时,会出现错误“无法解码JSON对象”。我做错了什么?我该怎么做?

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())

api手册在这里,但没有多大帮助:

http://www.scb.se/en_/About-us/Open-data-API/API-for-the-Statistical-Database-/


Tags: importapijsonhttpurlcodefilteritem