Norfeldt
Norfeldt

Reputation: 9688

Firebase emulator hitting DB via the REST feature

I’m trying to setup the emulator so I can develop the firebase functions safely before deploying them. I just noticed that some REST calls I’m doing now fails - anybody know if it is not possible to use the REST feature of the RealTime DB https://firebase.google.com/docs/reference/rest/database

I'm trying to hit it with this URL

http://localhost:9000/?ns=<PROJECT ID>-default-rtdb/development/DISHES.json

because this is what I set the firebaseConfig.databaseURL to (suggested here by Google)

Bonus info: If I try to do a GET to the URL via postman it creates another database called fake-server (http://localhost:4000/database/fake-server: null) 🤔

Upvotes: 1

Views: 361

Answers (1)

Yuchen Shi
Yuchen Shi

Reputation: 116

According to RFC 3986, the path must come before the query parameters in URLs. Your URL should be instead written as:

http://localhost:9000/development/DISHES.json?ns=<PROJECT ID>-default-rtdb

Note how the corrected URL has the query parameter appended to the very end. (The URL you've provided in the question will be parsed as having one query parameter ns with the value of <PROJECT ID>-default-rtdb/development/DISHES.json, which is not a valid namespace name. That explains the errors you've seen.)


FYI, it looks like you're constructing the URL by concatenating the string databaseURL with the path -- this may lead to surprising results as you've seen above. Considering using a URL parser / formatter in your language / framework of choice instead, which handles URL parts correctly. For example, in JavaScript you can use the snippet below:

const url = new URL(databaseURL); // Parse URL (including query params, if any)
url.pathname = '/development/DISHES.json';
yourFetchingLogic(url.toString()); // Reconstruct the URL with path replaced

Upvotes: 4

Related Questions