3

I want to upload a entire JSON file to my Firebase Database, but I can't get it to run. I am new to Python and Firebase.

This is my code so far:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
import json
import requests
from pprint import pprint



cred = credentials.Certificate("path/to/serviceAccountKey.json") 
firebase_admin.initialize_app(cred)



with open ('/Users/name/Desktop/Test.json') as data_file: data = json.load(data_file)
jsondata = json.dumps('/Users/name/Desktop/Test.json')



requests.put(url="https://myapp.firebaseio.com/", json= jsondata)

I get a error : <Response [400]>

1 Answer 1

3

Your code that performs the actual write operation:

requests.put(url="https://myapp.firebaseio.com/", json= jsondata)

This code does not in any way use the Admin SDK, but instead writes directly to the REST API of the Firebase Realtime Database.


If you want to write using the REST API, it is possible, but you'll need to:

  1. Ensure your REST call is authenticated, as shown in the documentation on authenticating REST requests.

  2. Use a URL that ends with .json to ensure your call end up to the REST API. So:

     requests.put(url="https://myapp.firebaseio.com/.json", json= jsondata)
    

    Yes, that /.json at the end of the URL is normal.


Alternatively you can continue to use the Admin SDK to write the data, but in that case you'll need to read and parse the JSON into your code, and send it to the database as a JavaScript object, as shown in the documentation on saving data with the Admin SDK.

Sign up to request clarification or add additional context in comments.

3 Comments

So I need to implement the REST API (requests) and than it should be working?
You mostly need to choose to either use the REST API or the Admin SDK. The way you're now mixing both won't work. I explain what the two options entail in the other two sections of my answer.
Good to hear @adri567!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.