且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在使用startAT的Firebase REST查询中使用url编码的字符串

更新时间:2022-10-22 11:40:03

看起来你可以用double-percents来编码百分之一。但是你必须对它们进行URL编码,所以不要用 %% 代替每个,你可以使用

$ b p
https://mydb.firebaseio.com/publishers/0000014d45a67fa3_4e93b3f/sites/0000014d45b0dc08_20173d1/pages.json?auth=authstringredacted;startAt=\"http%25%253A%25%252F%25% 252Fblog%25%25ZZexample%25%25ZZcom%25%252F_Test_Blog_2015_06; orderBy =$ key; limitToLast = 1


In our database we have URLs that are used as FireBase keys.
These keys are url encoded strings of the form:

http%3A%2F%2Fblog%ZZexample%ZZcom%2F_Test_Blog_2015_06

A GET request returns empty:

curl 'https://mydb.firebaseio.com/publishers/0000014d45a67fa3_4e93b3f/sites/0000014d45b0dc08_20173d1/pages.json?auth=authstringredacted;startAt="http%3A";orderBy="$key";limitToLast=1'

Whereas this returns an item (note the "startat" parameter difference):

curl 'https://mydb.firebaseio.com/publishers/0000014d45a67fa3_4e93b3f/sites/0000014d45b0dc08_20173d1/pages.json?auth=authstringredacted;startAt="http";orderBy="$key";limitToLast=1'

At first I thought it might be due to the url encoding of the query args, but using these don't work either:

startAt="http%253A"

startAt="http:";orderBy="$key";limitToLast=1'

I have a http request client so that it doesn't encode the query args, and it also doesn't work.

I am aware that it looks weird to depend on non url encoded query strings on a REST interface but Firebase depends on the query arg values being valid JSON. Note the extra "" around the query arg values.

Is there a workaround that makes the queries work?

The other alternative is to add another table that maps the URL keys to unique ids that do not have the encoding problem.

It looks like you can use double-percents to encode a percent. But you have to URL-encode them, so instead of using %% in place of each %, you would use %25%25.

So for your example, this URL should do the trick: https://mydb.firebaseio.com/publishers/0000014d45a67fa3_4e93b3f/sites/0000014d45b0dc08_20173d1/pages.json?auth=authstringredacted;startAt="http%25%253A%25%252F%25%252Fblog%25%25ZZexample%25%25ZZcom%25%252F_Test_Blog_2015_06";orderBy="$key";limitToLast=1