Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
Swift/Firebase: Can't send request data on a Firebase Cloud Function

I can't seem to send an identifier over a request body to Firebase from a Swift iOS app.

My javascript code works (I tested it in an API builder):

exports.numberOfVideos = functions.https.onRequest((request, response) => {
var attributionID = request.body.data.attributionID || "No attributionID found.";
functions.logger.info('[LOG]: request.body has a value of ' + JSON.stringify(request.body), {structuredData: true});
var db = admin.firestore();

db.collection("content").get().then(snapshot => {
    var total = 0;
    snapshot.forEach(doc => {
        if (doc.data().attributionID == attributionID) {
            total++;
        }
        // stuff = stuff.concat(newelement);
    });
    response.send('{"data": {"attributionID": "' + attributionID + '", "numberOfContents": "' + total + '"}}');
    return "";
}).catch(reason => {
    response.send(reason)
})
 });

My Swift code can't see to convey the data:

static func getContentCount(for obj: Object, completion: @escaping (_ count: Int?) -> Void) {
    let httpsCallable = functions.httpsCallable("numberOfVideos")

//        httpsCallable.setValue("application/json", forKey: "Content-Type")
    httpsCallable.call(["attributionID" : obj.id]) { result, error in
        if let error = error as NSError? {
            if error.domain == FunctionsErrorDomain {
                let code = FunctionsErrorCode(rawValue: error.code)
                let message = error.localizedDescription
                let details = error.userInfo[FunctionsErrorDetailsKey]
                print(error)
            }
        }
        

        if let response = result?.data as? [String: [String: Any]] {
            if let data = response["data"] {
                if let countAsAString = data["numberOfContents"] as? String {
                    let count = Int(countAsAString)
                    if count != nil {
                        completion(count)
                    } else {
                        print("Data is valid but no count found: \(result?.data)")
                        completion(nil)
                    }
                }
            }
        } else {
            print("Data is invalid: \(result?.data)")
            completion(nil)
        }
    }
}

When it worked from a REST API tester, here was the body:

POST /numberOfVideos HTTP/1.1
Host: *****************
Content-Type: application/json
Content-Length: 49

{"data":{"attributionID":"biNiaWVtmjUKoTQ1fTZu"}}

Any help would be appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Fixed it. I had to use an NSDictionary, not a Dictionary:

NSDictionary(dictionary: ["attributionID" : obj.id])
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda