Curious2learn
Curious2learn

Reputation: 33638

Push a raw value to Firebase via REST API

I am trying to use the requests library in Python to push data (a raw value) to a firebase location.

Say, I have urladd (the url of the location with authentication token). At the location, I want to push a string, say International. Based on the answer here, I tried

data = {'.value': 'International'}

p = requests.post(urladd, data = sjson.dumps(data))

I get <Response [400]>. p.text gives me:

u'{\n  "error" : "Invalid data; couldn\'t parse JSON object, array, or value. Perhaps you\'re using invalid characters in your key names."\n}\n'

It appears that they key .value is invalid. But that is what the answer linked above suggests. Any idea why this may not be working, or how I can do this through Python? There are no problems with connection or authentication because the following works. However, that pushes an object instead of a raw value.

data = {'name': 'International'}

p = requests.post(urladd, data = sjson.dumps(data))

Thanks for your help.

Upvotes: 2

Views: 1960

Answers (2)

Curious2learn
Curious2learn

Reputation: 33638

Andrew's answer above works. In case someone else wants to know how to do this using the requests library in Python, I thought this would be helpful.

import simplejson as sjson

data = sjson.dumps("International")

p = requests.post(urladd, data = data)

For some reason I had thought that the data had to be in a dictionary format before it is converted to stringified JSON version. That is not the case, and a simple string can be used as an input to sjson.dumps().

Upvotes: 2

Andrew Lee
Andrew Lee

Reputation: 10195

The answer you've linked is a special case for when you want to assign a priority to a value. In general, '.value' is an invalid name and will throw an error.

If you want to write just "International", you should write the stringified-JSON version of that data. I don't have a python example in front of me, but the curl command would be:

curl -X POST -d "\"International\"" https://...

Upvotes: 2

Related Questions