Python: POST request not working?
<p>Making a simple POST request to Firebase. For some reason, it's not working. cURL with the same data is working, no issues. Any ideas?</p>

<p>Code below:</p>

<pre><code>import requests    
r = requests.post("https://testapp-f55e1.firebaseio.com/test.json", data={"location":{"altitude":"200","latitude":"23.2", "longitude":"44.32"},"polution":{"pm10":"11","pm2":"123"}})
logging.debug(r)
</code></pre>

<p>It starts to work, but nothing happens. </p>

<pre><code>INFO:Posting to https://testapp-f55e1.firebaseio.com/test.json
</code></pre>

<p>The request doesn't reach Firebase.</p>

<p>If I do a curl request with the same URL, it works like a charm. Any ideas?</p>

<p>It expects <em>json</em> so replace <em>data=</em> with <em>json=</em>, requests will call <em>json.dumps</em> and set the headers for you:</p>

<pre><code>In [6]: import requests
   ...: r = requests.post("https://testapp-f55e1.firebaseio.com/test.json", json
   ...: ={"location":{"altitude":"200","latitude":"23.2", "longitude":"44.32"},"
   ...: polution":{"pm10":"11","pm2":"123"}})
   ...: print(r)
   ...: print(r.json())
   ...: 
&lt;Response [200]&gt;
{'name': '-KTpRAvBqP4Ra-FSXtKO'}
</code></pre>

<p>The output from using data= was giving you a clue:</p>

<pre><code>In [7]: import requests
   ...: r = requests.post("https://testapp-f55e1.firebaseio.com/test.json", data
   ...: ={"location":{"altitude":"200","latitude":"23.2", "longitude":"44.32"},"
   ...: polution":{"pm10":"11","pm2":"123"}})
   ...: print(r)
   ...: print(r.json())
   ...: 
&lt;Response [400]&gt;
{'error': "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."}
</code></pre>

python curl firebase python-requests