POST request works in Postman but not in Python
<p>When I make this POST request in Postman, I get the data. When I do it in Python 2.7 (using a Jupyter notebook), I get the error "No JSON object could be decoded". What am I doing wrong and how can I make it work?</p>

<pre><code>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())
</code></pre>

<p>The manual for the api is here, but it is not much help:</p>

<p><a href="http://www.scb.se/en_/About-us/Open-data-API/API-for-the-Statistical-Database-/" rel="nofollow">http://www.scb.se/en_/About-us/Open-data-API/API-for-the-Statistical-Database-/</a></p>

<p>Set <code>json=payload</code> and requests will add the headers you need:</p>

<pre><code>url = 'http://api.scb.se/OV0104/v1/doris/en/ssd/BE/BE0101/BE0101A/BefolkningNy'
payload = ....


r = requests.post(url, json=payload)
</code></pre>

<p>That will give you your json:</p>

<pre><code>In [7]: 
   ...: r = requests.post(url, json=payload)
   ...: print(r.json())
   ...: 
{u'data': [{u'values': [u'2054343'], u'key': [u'01', u'2010']}, {u'values': [u'2091473'], u'key': [u'01', u'2011']}], u'comments': [], u'columns': [{u'text': u'region', u'code': u'Region', u'type': u'd'}, {u'text': u'year', u'code': u'Tid', u'type': u't'}, {u'text': u'Population', u'code': u'BE0101N1', u'type': u'c'}]}
</code></pre>

<p>If you happen to get an <em>json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig):</em>  error set the encoding to <em>utf-8-sig</em>:</p>

<pre><code>r = requests.post(url, json=payload)
r.encoding = "utf-8-sig"
print(r.json())
</code></pre>

python json