如何使用 Vapor post Firebase 中的值?

How to post a value in Firebase using Vapor?

我遵循了 Vapor 的一些教程,尽管我了解如何在服务器上获取或放置或修补一些 JSON 值,但我不知道如何从应用程序内将字典发送到 Firebase。

我试过使用 Postman 在 Firebase 上写入一些数据,我没有问题,但我不知道如何使用 Vapor 处理请求的参数?

任何人都可以告诉我必须将数据库 URL 放在哪里以及如何发出请求吗?或者更好,如果 GitHub 上有一些例子,我可以看一下?

没有用于服务器端的 Firebase SDK-Swift 因此您可以使用 Firebase 的 REST API 处理 JSON 值。

See this topic.

您需要创建 JWT 令牌才能访问 Firebase 数据库。那是最难的部分。

let drop = Droplet()
func readUsersNodeInFb()  {

    let customer = userStripeIdCustomer
    do {
        let responseFirebase = try drop.client.get(
            "https://bev-708.firebaseio.com/Users/\(usersUID!)/\(customer!)/\(bookingNumber!).json", headers: ["Authorization":"Bearer \(TokenData.acessTokenReceived!)"], query: [:],
            body: " a JSON Object")


        responseFirebaseAssigned = responseFirebase
    } catch let error {

        let errorString = "error line 115 in ClaimBookingCleaner received when trying to update  Firebase, \(error.localizedDescription)"
        errorFirebaseReceived = errorString
        return
    }


    //response from Firebase
    let bodyReceived = responseFirebaseAssigned?.body.bytes

    do {
        let jsonFire = try JSON(bytes:bodyReceived!)
         JsonFirebase = jsonFire
         print("jsonFire is \(jsonFire)")
    } catch let error {

        let errorString = "jsonFire could not be built with the payload received from server, line 55 in ClaimBookingCleaner error \(String(describing: error.localizedDescription))"
        errorFirebaseReceived = errorString
        return
    }