简体   繁体   中英

Firebase Firestore REST example

Hello I am looking to write a script which uses firebase firestore and writes some json to a specific collection in firestore. I have done this with the realtime db but firestore is a tad different below is my Realtime db snippet that works.

curl -X POST \
-d '{"param1":"'"$1"'", "param2":"'"$2"'"}' \
https://xxxx.firebaseio.com/xxxx.json?

Thanks for the help

After Reading the documentation I got to this

curl -X POST \
-H "Content-Type: application/json" \
-d'{
"fields": {
"Field1": {
"stringValue": "'"$var1"'"
},
"Field2": {
"stringValue": "'"$var2"'"
},
"Field3": {
"stringValue": "$var3"
}
}
}'\"https://firestore.googleapis.com/v1beta1/projects/**PROJECT_ID**/databases/(default)/documents/**COLLECTION_ID**?&key=(YOUR API KEY)"

The accepted answer helped me, but it took me a long time to figure out how can I use data types other than stringValues , so I am adding this answer hoping someone finds this helpful in the future.

curl -X POST \
-H "Content-Type: application/json" \
-d' {
    "fields": {
        "Field1": {
            "arrayValue": {
                "values": [{
                    "mapValue": {
                        "fields": {
                            "key1": {
                                "stringValue": "val1"
                            },
                            "key2": {
                                "stringValue": "val2"
                            }
                        }

                    }
                }]
            }
        },
        "Field2": {
            "integerValue": <intValue>
        },
        "Field3": {
            "stringValue": "var3"
        }
    }
}'\"https://firestore.googleapis.com/v1beta1/projects/**PROJECT_ID**/databases/(default)/documents/**COLLECTION_ID**?&key=<YOUR WEB API KEY>"

Use this for reference.

example field value

var data = { "fields": { "productName":{"stringValue": dealname.toString()}, "companyname":{"stringValue": companyName.toString()}, "contact":{"stringValue": contact.toString()}, "email":{"stringValue": email.toString()}, "domain":{"stringValue": domain.toString()}, "createdate":{"stringValue": createdate.toString()}, "salesCode":{"stringValue": code.toString()}, "price":{"stringValue": amount.toString()}, "phone":{"stringValue": phone.toString()}, "orderId":{"stringValue": orderId.toString() } } };

more information firestore information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM